This question already has answers here:
How to get an absolute file path in Python
(11 answers)
Closed 2 years ago.
Let's say I have this path
D:\something\something1\from_here_I_now\stuff\stuff2.
So, I know that \from_here_I_now\stuff\stuff2 is a permanent path, but the beginning is different, like I know that D:\something\something1\ may be different for someone else. How can I find the D:\something\something1\ knowing only \from_here_I_now\stuff\stuff2?
Try something like this:
import os
filestr = '\from_here_I_now\stuff\stuff2'
fullstr = os.path.abspath(filestr)
print(fullstr)
>>> 'D:\something\something1\from_here_I_now\stuff\stuff2'
print(fullstr[:len(filestr)])
>>> 'D:\something\something1'
Related
This question already has answers here:
Python replace / with \
(3 answers)
Closed 8 months ago.
I would like to go the opposite direction of the post Python replace / with \.
For example, the path to desktop:
import os
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
print(desktop)
The above code prints "C:\Users\r2d2w\Desktop". I would like to convert it to "C:/Users/r2d2w/Desktop". How can this be done? The line desktop.replace("\", "/") will not work and will give error "EOL while scanning string literal".
Have you already tried to do:
desktop.replace("\\", "/")
?
This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 4 years ago.
I want to get the substring from a path from the end to a certain character, take for example the following path:
my_path = "/home/Desktop/file.txt"
My intention is to do something like:
my_path.substring(end,"/")
So I can get the name of the file that is located between the end of the string and the character "/", in this case "file.txt"
The easiest approach, IMHO, would be to split the string:
filename = my_path.split('/')[-1]
use the os.path.basename for this
In [1]: import os
In [2]: os.path.basename('/home/Desktop/file.txt')
Out[2]: 'file.txt'
This question already has answers here:
Python: Get URL path sections
(7 answers)
Closed 5 years ago.
I'm on Python 3.6.
How I can extract part of URL so I can use it as variable.
For example, the URL is http://example.com/comp/project.sec and I would like to get the project part without .sec as variable.
How can I achieve that?
url = "http://example.com/comp/project.sec"
project = url.split("/")[-1].split(".")[0]
Here's an alternative, which removes split requirement.
from os.path import splitext, basename
splitext(basename('http://example.com/comp/project.sec'))[0]
import urllib
urllib.parse.urlparse("http://example.com/comp/project.sec").path.split("/")[-1].split(".")[-1]
This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 5 years ago.
Is it possible to manipulate a variable, for example:
file = "/Python/work.txt"
to just list work.txt, without /Python? excluding everything on the left of the "/"?
Thank you
Of course! Simply do this:
file = "/Python/work.txt"
excluded = file.split("/")[-1]
This would return "work.txt" in the excluded variable.
This question already has answers here:
Canonicalize / normalize a URL?
(6 answers)
Closed 8 years ago.
Is there an os.path function to reduce a path to its simplest form? For example, is there a way to reduce /a/b/c/../../ to /a/?
I've tried os.path.abspath(url) but this prepends my home directory as well. For example, if I want www.google.com/a/index.html it returns /Users/myname/Documents/www.google.com/a/index.html
Obviously one possible solution is to remove /Users/myname/Documents/ from all paths that I create, but I feel like there must be a better way...
You could do something like this:
print(os.path.abspath("/a/b/c/../../") + os.path.sep)
print(os.path.normpath("/a/b/c/../../") + os.path.sep)
# both print /a/
If you work with real paths and python 3.4, you can also use pathlib:
import pathlib
print(pathlib.Path("/tmp/tmux-1000/../").resolve() )