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

qgis 3 - Python3 Replace "/" With "\" - Geographic Information Systems Stack Exchange
I want to import several spatial csv files (points) into QGIS. The path to the folder, where the csv files are located is stored in the variable "Folder_Path". The file names are stored in a list. ... More on gis.stackexchange.com
🌐 gis.stackexchange.com
July 16, 2019
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
python - How to convert back-slashes to forward-slashes? - Stack Overflow
The modifier r before the string tells Python that this is a raw string. In raw strings, the backslash is interpreted literally, not as an escape character. ... I have path = 'some\thing' and then in another function I access the variable path. I wish I could do rpath, but it doesn't work anymore. Is there a solution for this case? 2019-06-14T10:53:44.663Z+00:00 ... To define the path's variable you have to add r initially, then add the replace ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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()).
🌐
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...
🌐
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
🌐
Python Examples
pythonexamples.org › python-string-replace-forward-slash-with-backward-slash
Python String - Replace Forward Slash with Backward Slash
To replace the Forward slash (/) with Backward slash () in Python, you can use string replace() method. Call replace method on the given string, and pass forward slash string "/", and backward slash string "\" as arguments.
🌐
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
🌐
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?

🌐
Bobby Hadz
bobbyhadz.com › blog › python-remove-backslash-from-string
Remove Backslashes or Forward slashes from String in Python | bobbyhadz
April 10, 2024 - Copied!string = '/bobby/hadz/com/' print(string) # 👉️ /bobby/hadz/com/ # ✅ Remove leading and trailing forward slash from string new_string = string.strip('/') print(new_string) # 👉️ bobby/hadz/com # ------------------------------------------------------- # ✅ Remove leading and trailing backslash from string string = '\\bobby\\hadz\\com\\' print(string) # 👉️ \bobby\hadz\com\ new_string = string.strip('\\') print(new_string) # 👉️ bobby\hadz\com
🌐
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…
🌐
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 ...
🌐
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]
🌐
Reddit
reddit.com › r/python › how do you replace a character in a string with a single backslash?
r/Python on Reddit: How do you replace a character in a string with a single backslash?
March 16, 2015 -

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