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 Overflow
Discussions

regex - Python replace forward slash with back slash - Stack Overflow
The second argument will generate ... a single backslash, which is treated is the string "\'" ... @synaptik If you're seeing that in the python REPL, it prints escaped strings automatically. Try print '\\\\MYDIR\data\\abc' to get the value you seek ยท โ€“ Explosion Pills Commented Jan 31, 2013 at 21:25 ... I simply put a r in front of / to change the forward slash... More on stackoverflow.com
๐ŸŒ stackoverflow.com
file - How to replace backslash with forward slash - Python - Stack Overflow
I know that "" is an escape literal in Python but all I need is to scan through the text file and replace every single backlash with a forward slash "/". More on stackoverflow.com
๐ŸŒ stackoverflow.com
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?
If you add an "r" before your string the backslashs wont have any effect r'C:\text files\python\text files\reddit\test.txt' More on reddit.com
๐ŸŒ r/learnpython
25
76
March 23, 2019
Converting windows backslash to forward slash to use with fernet
I suggest using the pathlib. Whenever your drag&drop logic receives the filename strings, create a Path object for each. Then you can turn them in platform aware path strings and pass them to the rest of your logic/library with forward or backslashes accordingly. Please clarify if I missed the point. More on reddit.com
๐ŸŒ r/learnpython
3
1
February 1, 2020
๐ŸŒ
Codeigo
codeigo.com โ€บ home โ€บ replace forward slash with backslash in python
Replace Forward Slash With Backslash in Python - Codeigo
April 19, 2023 - A forward-slash (/) in a Python string can be replaced by a backslash (\) using the String replace() function, translate() method, or regular expression(re.sub()).
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Successfully Replace Backslashes with Forward Slashes in Python Strings - YouTube
Learn how to fix string replacement issues in Python when replacing backslashes with forward slashes in your file paths.---This video is based on the questio...
Published ย  April 7, 2025
Views ย  1
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ 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?
r/learnpython on Reddit: 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?
March 23, 2019 -

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!

Find elsewhere
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-string-replace
Python String Replace
March 25, 2025 - This program allows the user to enter the text, words to change, and the new text to substitute. Next, we used this Python string replace function to alter the old with the new. We are using the string replace function to change Backslash with ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ converting windows backslash to forward slash to use with fernet
r/learnpython on Reddit: Converting windows backslash to forward slash to use with fernet
February 1, 2020 -

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?

๐ŸŒ
w3tutorials
w3tutorials.net โ€บ blog โ€บ replace-backslashes-with-forward-slashes-in-python
How to Replace Backslashes with Forward Slashes in Python Paths: Fixing Escape Character Issues โ€” w3tutorials.net
If the escape sequence is valid but unintended, Python will misinterpret the path: # "C:\new_folder" is interpreted as "C:ew_folder" (because \n = newline) path = "C:\new_folder" print(path) # Output: C: # ew_folder ยท To avoid this, we need to either escape backslashes (with \\) or use methods to replace them with forward slashes.
๐ŸŒ
GitHub
github.com โ€บ philiporlando โ€บ replace_last_backslash
GitHub - philiporlando/replace_last_backslash: Python package with a single module that replaces the last backslash in a path string with a forward slash.
Import the replace_last_backslash ...path\\to/fix.txt" The replace_last_backslash function takes a path string as input and returns the modified path string with the last backslash replaced by a forward slash....
Author ย  philiporlando
๐ŸŒ
McNeel Forum
discourse.mcneel.com โ€บ scripting
Python - replace backslash in string - rs.documentpath() - Scripting - McNeel Forum
June 16, 2015 - Trying to replace the backslash character in a string obtained with the rs.documentpath() method. The string replace function will not work since the backslash is an escape character. Any way to change my string into a rโ€ฆ
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to replace forward slashes in a list?
r/learnpython on Reddit: How to replace forward slashes in a list?
March 9, 2021 -

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:

  1. zxc

  2. asd

  3. q/w/e

Need:

  1. zxc

  2. asd

  3. q//w//e

[sub.replace('\\', '\\\\') for sub in my_list]
๐ŸŒ
Python Forum
python-forum.io โ€บ thread-37781.html
change backslash into slash in a path
July 21, 2022 - Hi Typically in a Windows path, I want to change the special 'backslash' character to 'slash' one. I was thinking re.sub substitutes all occurences but whatever I've tested so far, it fails (same result using replace()): what I'm missing? Path = 'D...
๐ŸŒ
Quora
quora.com โ€บ How-do-you-change-a-double-backslash-to-a-single-back-slash-in-Python
How to change a double backslash to a single back slash in Python - Quora
Answer: This is an interesting one. A few important notes before my answer code: * To put a backslash in a string literal, type two backslashes. The first one indicates the start of an escape sequence; the second one indicates you want the escape sequence to produce a backslash. (If you wanted ...