You can use always:

'C:/mydir'

This works both in Linux and Windows.

Another possibility is:

'C:\\mydir'

If you have problems with some names you can also try raw string literals:

r'C:\mydir'

However, the best practice is to use the os.path module functions that always joins with the correct path separator (os.path.sep) for your OS:

os.path.join(mydir, myfile)

From python 3.4 you can also use the pathlib module. This is equivalent to the above:

pathlib.Path(mydir, myfile)

or:

pathlib.Path(mydir) / myfile
Answer from joaquin on Stack Overflow
🌐
Python
docs.python.org › 3 › library › os.path.html
os.path — Common pathname manipulations
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 does not do any automatic path expansions.
Discussions

Implement Path property that returns the full path as a string - Ideas - Discussions on Python.org
pathlib already provides multiple methods / properties which return things as a str object the reasoning is that It is kind of odd that you have a ton of properties that give you parts of the path, but no property that gives you the whole path pathlib.PurePath.name for example I propose an ... More on discuss.python.org
🌐 discuss.python.org
0
October 13, 2024
Help with getting file path to string
dirname probably doesn't do what you think it does. You might want to look into os.abspath I think More on reddit.com
🌐 r/learnpython
7
1
February 25, 2025
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 - Convert a str to path type? - Stack Overflow
Is there a way to convert a string to a path type? I've tried searching, but could only find the reverse case (path to string). I also tried using os.path.join(), but that returns a string as well. Edit: This is python 2.7, if that makes a difference. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
docs.python.org › 3 › library › pathlib.html
pathlib — Object-oriented filesystem paths
Return whether this path points to the same file as other_path, which can be either a Path object, or a string.
🌐
Python.org
discuss.python.org › ideas
Implement Path property that returns the full path as a string - Ideas - Discussions on Python.org
October 13, 2024 - pathlib already provides multiple methods / properties which return things as a str object the reasoning is that It is kind of odd that you have a ton of properties that give you parts of the path, but no property that gives you the whole path pathlib.PurePath.name for example I propose an Pathobj.fullpath or Pathobj.full_path property be added this should result in the same operation as str(Pathobj) so that Pathobj.resolve().full_path can also work
🌐
GeeksforGeeks
geeksforgeeks.org › python › create-a-file-path-with-variables-in-python
Create a File Path with Variables in Python - GeeksforGeeks
July 23, 2025 - The path is built by embedding variables inside a formatted string with / as a separator.
🌐
Reddit
reddit.com › r/learnpython › help with getting file path to string
r/learnpython on Reddit: Help with getting file path to string
February 25, 2025 -

Hello all, beginner here. Trying to write a small function that does the following:

  1. Reads the current directory of files

  2. Then allows the user to chose 1 file from the printed list

  3. Stores that chosen file's absolute filepath to a string

Source code below:

import os

filesFound = os.listdir(os.getcwd())

for num, file in enumerate(filesFound): 
        print(f"#{num}: {file}")
        filepath = filesFound[num]
        directory = os.path.dirname(filepath)

choice = int(input ("\nChoose one\n\n"))

print("Chosen: " + filesFound[choice] + "\n")
print("File chosen path is: \n" + os.path.dirname(filesFound[choice]))

Any help/corrections are greatly appreciated.

🌐
Sentry
sentry.io › sentry answers › python › handle windows paths in python
Python Windows Path: Fix Backslash Escape Errors | Sentry
July 3, 2026 - Resolve unicodeescape errors in Python Windows paths by using raw strings with the r prefix, forward slashes, or double backslashes in file path strings
Find elsewhere
🌐
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

🌐
Medium
medium.com › @guandika8 › stop-representing-file-paths-in-python-using-strings-8312791f10f
Stop representing file paths in Python using strings! | by Andika Wirawan | Medium
July 28, 2023 - It defines the abstract class Path and its concrete subclasses PosixPath and WindowsPath. The Path class can be created with a path-like object (such as a string or bytes) and provides methods and properties for accessing various aspects of the path.
🌐
Python Guides
pythonguides.com › convert-string-to-path-in-python
How to Convert String to Path in Python
April 14, 2025 - Learn how to convert a string to a file path in Python using the `pathlib` module. Use `Path("your/path/here")` to work with file paths in an object-oriented way
🌐
Luc
introcs-python.cs.luc.edu › files › paths.html
Path Strings — Introduction to Computer Science in Python: Principles and Practice (18 Jun 2026)
June 18, 2026 - A relative path starts from the current working directory and does not begin with a separator: data.txt (file in the current directory) reports/summary.txt (subdirectory of the current directory) ../other_project/run.py (one level up, then into another folder) .. means “the parent directory”; . means “the current directory.” · Windows uses \ and macOS/Linux use /. Hard-coding either character makes scripts fragile. Python’s pathlib module handles this automatically using the / operator to join path components:
🌐
b.telligent
btelligent.com › en › blog › best-practice-working-with-paths-in-python-part-1-2
Best Practice: Working With Paths In Python (Part 1)
August 25, 2026 - path_dir: str = "C:\\Users\sselt\Documents\\blog_demo" What bothers me besides the illegibility of this is that one does not use escape sequences at every character-separator combination, only before the “U” and “b”. Use raw strings with “r” as a prefix to indicate that special characters should not be evaluated.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Get the Filename, Directory, Extension from a Path String in Python | note.nkmk.me
May 8, 2023 - In Python, you can get the filename (basename), directory (folder) name, and extension from a path string or join the strings to generate the path string with the os.path module in the standard library.
🌐
Towards Data Science
towardsdatascience.com › home › data science › path representation in python
Path Representation in Python | Towards Data Science
August 20, 2024 - When it comes to handling file paths in Python, pathlib offers a range of features that go beyond just fixing the shortcomings of strings and the os module.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use pathlib in Python | note.nkmk.me
February 9, 2024 - You can create a Path object using the pathlib.Path() constructor by specifying a path string.
🌐
Real Python
realpython.com › videos › creating-path-objects-from-strings
Creating Path Objects From Strings (Video) – Real Python
03:24 You can do that and the pathlib library knows what to do with that, and then correctly creates a Path object that points to this specific path. 03:33 Another way of doing it is to use raw string, and you can do that in Python by prefixing the string with just the r character, so you put this r in front of the string, and then you can use \ characters like you would normally with a Windows path.
Published: December 20, 2022
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python how-to's" › "how to set file path in python"
How to Set File Path in Python | Delft Stack
September 12, 2021 - A path is a string or path object that identifies a file or directory location. To specify a Windows path in Python, escape backslashes or use a raw string; to combine components, prefer pathlib.Path or use os.path.join().
🌐
Fabienmaussion
fabienmaussion.info › scientific_programming › week_03 › 02-Strings-Paths.html
String formatting and file paths — Scientific Programming
Often, it also implies writing new files (for example with post-processed data). Fortunately, python comes with many handy tools to format strings. “String formatting” refers to formatting the content of variables (strings, numbers, paths, etc.) into strings, for example to display them ...