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
Related
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 15 days ago.
enter image description here
i use Python and Visual Studio Code IDE
Is there a way to fix this?
Backslashes in strings are ”escape characters” used to signify special characters such as newline (\n), tab (\t), and so on.
To put an actual backslash in a string, use a double backslash: this\\that, or use an r-string: r”this\that”.
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 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('\\', '/')
This question already has answers here:
How can I remove a trailing newline?
(27 answers)
Closed 6 years ago.
i have some scripts that take as input one or multiple paths as on argument.
The script is run like that: myScript.py D:\Folder1,E:\OtherData\Files
In the script, i split the path arguments in the comma and i read the paths.
The problem is that Python adds a \r in the end of each path for no reason.
So the script tries to read D:\Folder1\r and E:\OtherData\Files\r.
Why is this and how can i solve it?
There are many ways, here is one for example.
Replacing the relevant problematic string with an empty string:
paths= [x.rstrip() for x in paths_list]
The method rstrip() returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).
This question already has an answer here:
Python 3.4.1 script syntax error, arcpy & [duplicate]
(1 answer)
Closed 7 years ago.
I am very new to Python and am getting this small problem. I am using Python 3.3
There is a variable I declared in my code
file_name = "resource\email\ham\6.txt"
However, when I look for the variable, it appends additional numbers
>>file_name
'resource\\email\\ham\x06.txt'
Is there a reason why it behaves as so? If not, how do I remove those additional characters? Also, why are they there?
Use r raw string:
file_name = r"resource\email\ham\6.txt"
Double \:
file_name = "resource\\email\\ham\\6.txt"
Or /:
file_name = "resource/email/ham/6.txt"
\ has a special meaning in python, it is used to escape characters.