Elaborating this answer, with pathlib you can use the as_posix method:
>>> import pathlib
>>> p = pathlib.PureWindowsPath(r'\dir\anotherdir\foodir\more')
>>> print(p)
\dir\anotherdir\foodir\more
>>> print(p.as_posix())
/dir/anotherdir/foodir/more
>>> str(p)
'\\dir\\anotherdir\\foodir\\more'
>>> str(p.as_posix())
'/dir/anotherdir/foodir/more'
Answer from Stefano M on Stack OverflowElaborating this answer, with pathlib you can use the as_posix method:
>>> import pathlib
>>> p = pathlib.PureWindowsPath(r'\dir\anotherdir\foodir\more')
>>> print(p)
\dir\anotherdir\foodir\more
>>> print(p.as_posix())
/dir/anotherdir/foodir/more
>>> str(p)
'\\dir\\anotherdir\\foodir\\more'
>>> str(p.as_posix())
'/dir/anotherdir/foodir/more'
Doesn't this work:
>>> s = 'a\\b'
>>> s
'a\\b'
>>> print s
a\b
>>> s.replace('\\','/')
'a/b'
?
EDIT:
Of course this is a string-based solution, and using os.path is wiser if you're dealing with filesystem paths.
qgis 3 - Python3 Replace "/" With "\" - Geographic Information Systems Stack Exchange
file - How to replace backslash with forward slash - Python - Stack Overflow
Windows uses a backslash (\) instead of a forward slash (/) when displaying file paths. Is it possible to open a file without manually converting '\' to '/' or '\' to '\\' in order to open a file?
python - How to convert back-slashes to forward-slashes? - Stack Overflow
Trying to create a script that prompts a user with a file path but want to replace all back slashes with forward slashes to avoid any escaped characters when doing things with that file.
I’m in python 3.10 if needed.
If I just copy the location from the file and paste it into my code like this:
with open('C:\text files\python\text files\reddit\test.txt') as file_object:
contents = file_object.read()
print(contents)I get the following error:
OSError: [Errno 22] Invalid argument: 'C:\text files\\python\text files\reddit\test.txt'
I know I can manually change the backslashes to double back slashes in order to open the file:
with open('C:\\text files\\python\\text files\\reddit\\test.txt') as file_object:or I can convert the backslashes to forward slashes:
with open('C:/text files/python/text files/reddit/test.txt') as file_object:And everything will work just fine.
However, when getting the full path of a file it's very easy to just right click > properties > copy Location and then paste it into my code. Doing this means I always have to manually convert single backslashes to double backslashes or to forward slashes.
Is there anything I can do to keep the original file format without converting the backslashes in the original file path?
Thanks!
I recently found this and thought worth sharing:
import os
path = "C:\\temp\myFolder\example\\"
newPath = path.replace(os.sep, '/')
print(newPath) # -> C:/temp/myFolder/example/
Your specific problem is the order and escaping of your replace arguments, should be
s.replace('\\', '/')
Then there's:
posixpath.join(*s.split('\\'))
Which on a *nix platform is equivalent to:
os.path.join(*s.split('\\'))
But don't rely on that on Windows because it will prefer the platform-specific separator. Also:
Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
I am trying to make myself a basic encryption program (Similar to AxCrypt but more basic) and the whole idea is that you can just Shift+Click on the file you want to encrypt, and then you can paste it into the GUIs textbox, now for fernet to be able to encrypt it, it needs for me to remove the "" from when you shift click on the file, that works fine, however I cannot seem to use
filetoencrypt = file.replace("\\", "/") as it only removes the first backslash, I also cannot use r'C:\file\path' because as I said, I entered my file path in a gui. I have read a number of sites on google, and none of them seem to answer my question, could someone please give me some help?
As title states, looking for help on replacing single forward slashes as they appear in a list with double forward slashes. Not every value in the list will have them but want to replace when they occur. I have the below but all it is doing is removing the entire list.
List Example:
-
zxc
-
asd
-
q/w/e
Need:
-
zxc
-
asd
-
q//w//e
[sub.replace('\\', '\\\\') for sub in my_list]The goal: 'apple' -> 'app\e'
This doesn't work:
>>> "apple".replace('l', '\\')
'app\\e'This also doesn't work:
>>> "apple".replace('l', '\')
File "<stdin>", line 1
"apple".replace('l', '\')
^
SyntaxError: EOL while scanning string literal