If you are running your code on a *nix machine, you can use the PureWindowsPath class:

>>> from pathlib import PureWindowsPath, PurePosixPath
>>> path = PureWindowsPath('C:\\Users\\foo\\bar')

>>> path.parts
('c:\\', 'Users', 'foo', 'bar')

>>> PurePosixPath('/usr', *path.parts[2:])
PurePosixPath('/usr/foo/bar')

You can apply the string replace method to every line in a text file as follows:

with open("input.csv", "r") as f_in:
    with open("output.csv", "w") as f_out:
        for line in f_in:
            new_line = line.replace(...)  # magic goes here
            f_out.write("{}\n".format(new_line))
Answer from Selcuk on Stack Overflow
Discussions

Using absolute unix paths in windows with python - Stack Overflow
I chose abspath() intentionally ... letter to the returned path. It was hardly an accident. The drive letter will be the one on the current directory where the Python script lives (I'll update my answer accordingly). 2012-10-31T18:47:58.24Z+00:00 ... doesn't "do the right thing" when the input is a Windows path and you're running on Linux (in other words, it doesn't convert forward slashes ... More on stackoverflow.com
🌐 stackoverflow.com
Windows path to Python path
I wonder is there a better solution on the issue to convert windows path to python path? Yes; simply use pathlib. from pathlib import Path win_path = r"" path_universal = Path(win_path) print("Your Python Path: ", path_universal) The more you use it, the more you regret not knowing about it earlier. It's an awesome piece of kit in the standard library, probably my favourite. More on reddit.com
🌐 r/learnpython
4
2
February 2, 2022
python - How do I normalize a path format to Unix-style while on Windows? - Stack Overflow
I am storing paths in a json file using a python script. I want the paths to be stored in the same format (Unix-style) no matter which OS the script is run on. So basically I want to run the os.path.normpath function, but it only converts paths to Unix-style instead of changing its function depending on the host OS. What is the best way to do this? ... You can convert Windows... More on stackoverflow.com
🌐 stackoverflow.com
Convert Unix path to Windows path in a custom function?
I'm trying to write an "open" function that opens a given directory in File Explorer, but I'm running in to a semi-major issue. Specifying paths to explorer.exe is a pain, because it doesn't accept relative paths (you can enter explorer.... More on github.com
🌐 github.com
22
March 30, 2017
🌐
GitHub
github.com › consis-tency › WinLin-Path
GitHub - sjnakib/WinLin-Path: This is a simple Python script that allows you to convert Windows file paths to their corresponding Linux format. It replaces backslashes with forward slashes and converts drive letters to the appropriate Linux format. · GitHub
This is a simple Python script that allows you to convert Windows file paths to their corresponding Linux format. It replaces backslashes with forward slashes and converts drive letters to the appropriate Linux format. - sjnakib/WinLin-Path
Author   sjnakib
🌐
YouTube
youtube.com › mo'men ahmed
Python Convert Windows path to Unix path and vice versa - YouTube
How to convert windows path to UNIX in Python.
Published   September 8, 2019
Views   173
🌐
PyPI
pypi.org › project › wsl-path-converter
wsl-path-converter · PyPI
This converter works with Windows path mounted in WSL, either through /etc/fstab or through the mount command. The executable is called wpc. To convert a Windows path to its Linux counterpart, run it with the -u option:
      » pip install wsl-path-converter
    
Published   Aug 05, 2019
Version   0.3.1
🌐
Medium
medium.com › @ageitgey › python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f
Python 3 Quick Tip: The easy way to deal with file paths on Windows, Mac and Linux | by Adam Geitgey | Medium
January 31, 2018 - And if that’s all pathlib did, ... to Python — but it does a lot more! For example, we can read the contents of a text file without having to mess with opening and closing the file: Pro-tip: The previous examples were buggy because the opened file was never closed. This syntax avoids that bug entirely. In fact, pathlib makes most standard file operations quick and easy: You can even use pathlib to explicitly convert a Unix path into a Windows-formatted ...
Find elsewhere
🌐
Python
docs.python.org › 3 › library › os.path.html
os.path — Common pathname manipulations
Source code: Lib/genericpath.py, ... (for Windows). This module implements some useful functions on pathnames. To read or write files see open(), and for accessing the filesystem see the os module. The path parameters can be passed as strings, or bytes, or any object implementing the os.PathLike protocol. Unlike a Unix shell, Python ...
🌐
Python
python-list.python.narkive.com › WPytiwa0 › convert-between-windows-style-paths-and-posix-style-paths
Convert between Windows style paths and POSIX style paths
I have modules that do this in Windows & Cygwin Python, if you (meaning the OP) are interested. Another way is to access the registry and try to duplicate Cygwin's logic. I tried this but gave up on it--life's too short for that. Or you try to adapt the Cygwin C code. Or try to access the functions in cygwin.dll. Whatever. On an NT system without Cygwin, or on a Unix system, then there is no obvious mapping and (as far I can see) no need for one. Why do you want to use one style of path in your config files?
🌐
CopyProgramming
copyprogramming.com › howto › python-script-for-changing-windows-path-to-unix-path
Cross-Platform File Paths in Python: Converting Windows to Unix Paths with pathlib - Unix paths that work for any platform in python
November 29, 2025 - By adopting these best practices—using pathlib exclusively, leveraging Path objects, testing on multiple platforms, and leveraging recent Python features—you ensure your code remains maintainable, readable, and functional across Windows, macOS, and Linux systems. The future of Python development is cross-platform by design, and pathlib is the tool that makes it possible. ... unix paths that work for any platform in python file paths in python converting windows to unix paths
🌐
Reddit
reddit.com › r/learnpython › windows path to python path
r/learnpython on Reddit: Windows path to Python path
February 2, 2022 -

I have been searching the web for a solution without a result.
I made this small script that can be used. Feel free to use it.
I wonder is there a better solution on the issue to convert windows path to python path?

"""
A path, in Windows "C:\Folder" . in Python it is "C:/Folder".
In Python the "\" character can get interpreted as the ESCAPE character.
When an 'r' or 'R' prefix is present, a character following a backslash is
included in the string without change, and all backslashes are left in the string.
"""
def main():
# - YOUR INPUT - IMPORTANT: keep the r in front of path string here.
win_path = r"<TYPE YOUR PATH HERE>"
# Call function to convert from windows to Python path.
path_py = py_path(win_path)
print("Your Python Path: ", path_py)

def py_path(win_path):
python_path = "" # The result of this script.
# Convert to ASCII list
ascii_values_list = []
for character in win_path:
ascii_values_list.append(ord(character))
# Replace all ASCII values for "\" (=92) with value for "/" (=47).
for i in range(0, len(ascii_values_list)):
if ascii_values_list[i] == 92:
ascii_values_list[i] = 47
path_py = "" # Convert ASCII list to string
for val in ascii_values_list:
path_py = path_py + chr(val)

if path_py[-1] != "/": # Add "/" at end of path if needed.
path_py = path_py + "/"
return path_py

if __name__ == "__main__":
main() # Script goes there.

# EOF

🌐
Pythondex
pythondex.com › convert-windows-path-to-linux-in-python
Convert Windows Path To Linux In Python - Pythondex
January 31, 2024 - As you can see it just 3 lines of code because we used pathlib module to convert windows path to linux, After running this program you will see it printing the path in linux format like below
🌐
Post.Byes
post.bytes.com › home › forum › topic › python
windows / unix path - Post.Byes - Bytes
> os.path.join('P ictures', '01.jpg') returns 'Pictures\\01.. jpg' on Win. When I read files created on Win under Unix this is a problem, python cannot open 'Pictures\\01.j pg' > Thanks, > Marcin I use posixpath when I want to "force" forward slashes that I know will work on Linux. Actually the forward slashes work fine on Windows also (undocumented feature of Windows). -Larry ... Is there an built-in functionality in python to convert Windows paths to Unix paths?
🌐
Ask Ubuntu
askubuntu.com › questions › 1356011 › how-can-i-convert-a-windows-path-to-unix
bash - How can I convert a Windows path to Unix? - Ask Ubuntu
August 4, 2021 - The converted path is the destination: ... I hope for helpful answers, because i am already desperate 🤓. ... I know the version with wsl path but, for my program to work I need to save the path in a variable, how can I do that ? ... This should be able to accomplish what you are after by storing the Windows path as a Unix/Linux path variable in WSL:
🌐
GitHub
github.com › Microsoft › WSL › issues › 1834
Convert Unix path to Windows path in a custom function? · Issue #1834 · microsoft/WSL
March 30, 2017 - You switched accounts on another tab or window. Reload to refresh your session. ... I'm trying to write an "open" function that opens a given directory in File Explorer, but I'm running in to a semi-major issue. Specifying paths to explorer.exe is a pain, because it doesn't accept relative paths (you can enter explorer.exe example-folder, but not explorer.exe example-folder/test). My idea is to take a given Unix path, expand it to it's full path, and then convert it to a Windows path to pass to explorer.exe.
Author   JacobDB
Top answer
1 of 6
111

Windows Build 17046 [1] contains new wslpath utility that can translate paths from/to WSL/Windows. This was known missing WSL feature. [2]

Example usage:

$ echo $0
/bin/bash

$ which wslpath
/bin/wslpath

$ wslpath -a 'C:\\aaa\\bbb\\ccc\\foo.zip'
/mnt/c/aaa/bbb/ccc/foo.zip

You can call wslpath from Powershell on Windows:

>>> wsl wslpath -a 'C:\\aaa\\bbb\\ccc\\foo.zip'
/mnt/c/aaa/bbb/ccc/foo.zip

wslpath options and parameters:

-a    force result to absolute path format
-u    translate from a Windows path to a WSL path (default)
-w    translate from a WSL path to a Windows path
-m    translate from a WSL path to a Windows path, with ‘/’ instead of ‘\\’

Edit (05/24)

If you need the opposite conversion, i.e. to get a path suitable for WSL in PowerShell, you can use:

$pathInWSL = wsl wslpath -a -u "$(Get-Location)\foo.txt".Replace('\', '\\')
# "$(Get-Location)\foo.txt".Replace('\', '\\') = C:\\bar\\baz\\foo.txt
# $pathInWSL = /mnt/c/bar/baz/foo.txt

wsl cat $pathInWSL
# content of /mnt/c/bar/baz/foo.txt
2 of 6
7

I wrote a bat file to do this. Just place the file wherever you are working or add it to your path (or just put it above your code, which would be easier to work with). Remember to assign "variable" to your file path first (if you are using a separate file, try using parameters).

What the code does:

1) Get the first letter of the path, which is the drive.

2) Remove the first two letters.

3) Change the slashes.

4) This is the tricky part: since Linux is case sensitive, we need to convert uppercase drive letter to lowercase. Do this by matching each (tell me if there is a better way). You can remove unnecessary drive letters too, since you probably have no more than ten drives.

5) Combine everything to give the final string.

The result:

Input:

E:\myfiles\app1\data\file.csv

Output (with the quotation marks):

"/mnt/e/myfiles/app1/data/file.csv"

The code is as follows:

@echo OFF

set "variable=E:\myfiles\app1\data\file.csv"

set "drive=%variable:~0,1%"

set variable=%variable:~2%
set "variable=%variable:\=/%"

if %drive%==A set "drive=a"
if %drive%==B set "drive=b"
if %drive%==C set "drive=c"
if %drive%==D set "drive=d"
if %drive%==E set "drive=e"
if %drive%==F set "drive=f"
if %drive%==G set "drive=g"
if %drive%==H set "drive=h"
if %drive%==I set "drive=i"
if %drive%==J set "drive=j"
if %drive%==K set "drive=k"
if %drive%==L set "drive=l"
if %drive%==M set "drive=m"
if %drive%==N set "drive=n"
if %drive%==O set "drive=o"
if %drive%==P set "drive=p"
if %drive%==Q set "drive=q"
if %drive%==R set "drive=r"
if %drive%==S set "drive=s"
if %drive%==T set "drive=t"
if %drive%==U set "drive=u"
if %drive%==V set "drive=v"
if %drive%==W set "drive=w"
if %drive%==X set "drive=x"
if %drive%==Y set "drive=y"
if %drive%==Z set "drive=z"

set "variable=/mnt/%drive%%variable%"

echo "%variable%"

@echo ON
🌐
Tech-Artists.Org
tech-artists.org › t › python-handling-windows-path-with-characters › 12045
Python - handling windows path with '\' characters - Tech-Artists.Org
December 9, 2019 - Hi, I’ve been struggling with windows path for a while because of Python regex. When I need my user to input a path variable in a tool, if they give a classical windows path Python will convert ‘’ because of regex. os.…