Changing `\` to `/` [duplicate] - python

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("\\", "/")
?

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

How can I find full path for a file? [duplicate]

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'

How to concatenate strings to build full path? [duplicate]

This question already has answers here:
Function to concatenate paths?
(2 answers)
String formatting in Python [duplicate]
(14 answers)
How to concatenate a fixed string and a variable in Python
(6 answers)
Closed 2 years ago.
path = r'C:\Program Files\1\1.exe'
I can run a program using the path above. I don't want to hardcode it, so I'm trying to use a variable in place of the 1s. This is what I came up with:
variable1 = '1'
path = r'C:\Program Files\ , +variable1 , +\, +variable1, +.exe'
This doesn't work. How do I make it work?
variable1 = '1'
path = 'C:\\Program Files\\'+variable1+'\\'+variable1+'.exe'
\ is special char so you need to escape it with another \

Python adds x0 to integers [duplicate]

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.

Categories