Python path separator [duplicate] - python

This question already has answers here:
How to get the PATH environment-variable separator in Python?
(5 answers)
Closed 4 years ago.
Is there a proper platform-dependent (or independent?) path separator character somewhere in the Python standard library?
I am not asking about the directory separator / and \, but rather about the PATH separator: : and ;.
Java has java.io.File.pathSeparatorChar and java.io.File.pathSeparator to fulfill this need. Does Python have something similar? A fairly exhaustive search in os.path and pathlib docs yielded nothing, so I am beginning to lose hope.
I am not particularly hung up on the character itself. A function that behaves like os.path.join but for entire paths instead of path elements would be perfectly acceptable, preferable even.

Maybe this is what you're looking for:
import os
os.pathsep
os.pathsep is : or ; while os.path.sep is \ or /.

Related

Adding a absolute path that has a \f in it [duplicate]

This question already has answers here:
How can I put an actual backslash in a string literal (not use it for an escape sequence)?
(4 answers)
Closed 3 months ago.
While adding an absolute path to my script because it has a \f in it the code won't run properly.
C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx
The file is in the same directory as the script but using a relative path won't work. No misspellings or anything.
Use python r string
path=r'C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx'
Use one of the following ways:
r"C:\Users\showoi\Desktop\website\repository\fileAdder\softwarelisting.xlsx"
"C:\\Users\\showoi\\Desktop\\website\\repository\\fileAdder\\softwarelisting.xlsx"
"C:/Users/showoi/Desktop/website/repository/fileAdder/softwarelisting.xlsx

How to get the current directory in python? [duplicate]

This question already has answers here:
How to convert back-slashes to forward-slashes?
(9 answers)
Closed 1 year ago.
I'm trying to get the current directory using the OS module, like that:
directory=os.getcwd()
However, when I do this, the directory comes with \, like 'C:\Users\...', and I need to use directory with \\ or /.
Is that anyway to get the directory with \\ or /?
You can just replace the \ with /. Note that the former must be escaped with another \, like below:
import os
directory = os.getcwd().replace('\\', '/')

for what use dot after comand "ls" in linux? ~ ls [duplicate]

This question already has answers here:
What is double dot(..) and single dot(.) in Linux?
(4 answers)
Closed 2 years ago.
using the command ~ls allows you to list files, which is difference if I add dot ~ls . ?
sorry for my english, I am practicing
dot shows the folder you are in. If u re in downloads. ls . shows downloads. In your situation, ls (without a directory argument) is going to list directories and files under the current directory(pwd).

Get substring from end to certain character in python [duplicate]

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'

Reducing a path to simplest value? [duplicate]

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() )

Categories