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 › pathlib.html
pathlib — Object-oriented filesystem paths
Source code: Lib/pathlib/ This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which pro...
Discussions

Total Noob - How do I properly refer to a file path in python?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
21
11
June 30, 2023
Path in python
The PATH Environment Variable lists all the places windows will look for a script or exe when you run it in the command prompt. cmd can only run scripts that are in the current folder, or things in the PATH. So if python.exe is not in your current directory in cmd, it won’t see that file. Adding its location to PATH tells cmd to also look there for python.exe, which allows cmd to find it, no matter what folder is open. Pip lives in a subfolder, so that also needs to be listed in the PATH variable. More on reddit.com
🌐 r/learnpython
10
1
February 16, 2020
Best Python Learning Path
This really depends on your learning style. If you are the type to read, then Automate Boring Stuff or Python Crash Course is a good start. If you want a video, then search for CS50P or 100 Days of Python by Angela Yu in Udemy. You can combine both since the book also explains some concepts in detail More on reddit.com
🌐 r/learnpython
11
2
October 30, 2023
🌐
CodingNomads
codingnomads.com › python-path-and-pathlib
Python Path and `pathlib`
The relative path of a file is the path from the Python script to the file.
🌐
GeeksforGeeks
geeksforgeeks.org › python › pathlib-module-in-python
Pathlib Module in Python - GeeksforGeeks
1. Path class: It represents a path and allows for a variety of methods that interact directly with the filesystem. Depending on system Python is running on, Path will behave like either PosixPath or WindowsPath.
Published: June 15, 2026
🌐
DataCamp
datacamp.com › tutorial › comprehensive-tutorial-on-using-pathlib-in-python-for-file-system-manipulation
How to Use Python's Pathlib (with Examples) | DataCamp
May 22, 2024 - Luckily, in Python version 3.4, developers introduced the pathlib module to the standard library. pathlib provides an elegant solution to handling file system paths using a long-awaited object-oriented approach, and it also ensures platform-agnostic behavior.
🌐
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.
Find elsewhere
🌐
W3Schools
w3schools.com › python › ref_module_pathlib.asp
Python pathlib Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training · ❮ Standard Library Modules · Work with paths using objects instead of strings: from pathlib import Path p = Path("/home/user") / "projects" / "demo.txt" print(p.name) print(p.parent) print(p.suffix) Try it Yourself » ·
🌐
Towards Data Science
towardsdatascience.com › home › data science › path representation in python
Path Representation in Python | Towards Data Science
August 20, 2024 - That's where pathlib comes in. It's part of Python's standard library and is designed to make filesystem path manipulation a breeze.
🌐
GeeksforGeeks
geeksforgeeks.org › python › pythonpath-environment-variable-in-python
PYTHONPATH Environment Variable in Python - GeeksforGeeks
January 9, 2026 - PYTHONPATH is an environment variable that tells Python where to look for modules and packages beyond the standard library and installed site-packages.
🌐
Cloudinary
cloudinary.com › home › how to add python to path – and why it matters
How to Add Python to PATH – and Why It Matters
August 1, 2025 - PATH is an environment variable, basically a list of folders your system searches when you run a command in the terminal. When you type python in your terminal, your system looks through each directory in the PATH list and tries to find an ...
🌐
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 - The simple command os.listdir lists all strings, i.e., only the path filenames. Here and in all other examples, I use type hinting for additional code documentation. This syntax became available from Python 3.5 onwards.
🌐
Readthedocs
pathlib.readthedocs.io
pathlib — pathlib 1.0.1 documentation
Manipulating filesystem paths as string objects can quickly become cumbersome: multiple calls to os.path.join() or os.path.dirname(), etc. This module offers a set of classes featuring all the common operations on paths in an easy, object-oriented way. This module is best used with Python 3.2 or later, but it is also compatible with Python 2.6 and 2.7.
🌐
Switowski
switowski.com › blog › pathlib
Pathlib for Path Manipulations
July 17, 2023 - Path.is_file is around twice as slow when the file exists (4.12/2.28≈1.81) and around three times as slow when it doesn't exist (2.82/1.02≈2.76). ... $ python -m timeit -s "import os" "os.getcwd()" 50000 loops, best of 5: 6.75 usec per loop $ python -m timeit -s "from pathlib import Path" "Path.cwd()" 50000 loops, best of 5: 8.54 usec per loop
🌐
Reddit
reddit.com › r/learnprogramming › total noob - how do i properly refer to a file path in python?
r/learnprogramming on Reddit: Total Noob - How do I properly refer to a file path in python?
June 30, 2023 -

Hello,

I am trying to write in the path "c:/gpt-2-2/models" into a python code. When I run the code I get a syntax error at the "C:" part. I guess I don't know how to properly write file paths in python. Any help would be appreciated.

🌐
IONOS
ionos.com › digital guide › websites › web development › python pathlib
How does Python pathlib work and what are its benefits?
June 18, 2025 - The module is par­tic­u­lar­ly useful if you not only want to read or edit the paths, but also perform ad­di­tion­al tasks and work steps. Among other things, it also allows you to create and copy in­di­vid­ual files and their com­po­nents. This is sim­pli­fied above all by Python’s su­per­or­di­nate interface pathlib.
🌐
YouTube
youtube.com › real python
Introduction to File Paths and Python's pathlib: Python Basics - YouTube
How do you create file paths with Python? Using the pathlib module simplifies accessing file paths on Windows, Mac, and Linux systems. In this portion of our...
Published: December 22, 2022
Views: 5K
🌐
YouTube
youtube.com › watch
Python's pathlib Module - YouTube
Python's standard library pathlib module can be used to manipulate paths, and work with the file system independently of the specific operating system the Py...
Published: September 16, 2022
🌐
Johnny-lin
johnny-lin.com › cdat_tips › tips_pylang › path.html
Appending to Your Python Path
Your path (i.e. the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module.
🌐
GeeksforGeeks
geeksforgeeks.org › python › os-path-module-python
OS Path Module in Python - GeeksforGeeks
July 1, 2026 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 1 Jul, 2026 · OS Path module provides functions for working with file and directory paths.