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('\\', '/')
Related
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
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:
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).
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:
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 /.