HOWTO · Python
How to Set File Path in Python
This tutorial demonstrates how to set the file path in Python for Windows users.
On this page
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(). Constructing a path does not open a file, check that it exists, execute it, or change Python’s import search path.
Escape Backslashes in Windows Path Strings
In an ordinary Python string, a backslash starts an escape sequence. Double each backslash in a Windows path, as in "C:\\Users\\Ada\\report.csv". This keeps sequences such as \n from being interpreted as newline characters. The doubled form is Python source notation; the resulting string contains one backslash at each separator.
Use Raw String Literals for Windows Paths
Prefix a string literal with r to have Python retain backslashes as written: r"C:\Users\Ada\report.csv". A raw string still follows Python’s string-literal syntax and cannot end with an odd number of backslashes, because the final backslash would escape the closing quote. If a directory path needs a final separator, join another component or use a normal string with escaped backslashes.
Build Paths with os.path.join()
os.path is a module, and os.path.join() is its function for joining path components. See the os.path API. Pass directory and filename as separate components, for example os.path.join("reports", "annual", "summary.csv"). The function uses the path conventions of the running operating system; it does not locate a file or select a project directory. An absolute later component can also change the result, so pass the intended components deliberately.
Build Paths with pathlib.Path
For ordinary path construction, pathlib.Path provides an object-oriented interface. Create a path from a string and join further components with /. This example uses a relative path and prints the constructed value; it does not access the file. The output below is the POSIX runtime result: Windows uses backslashes when displaying the same Path expression.
from pathlib import Path
report_path = Path("reports") / "annual" / "summary.csv"
print(report_path)
reports/annual/summary.csv
The displayed path spelling depends on the operating system. Path represents paths using the conventions of the current platform; use PureWindowsPath when you need to manipulate Windows-style paths on another operating system without accessing the filesystem.
Construct Absolute Paths
An absolute path identifies a location from a filesystem root. On Windows, a drive-qualified example is Path(r"C:\Users\Ada") / "reports" / "summary.csv"; the same spelling is not an absolute path on every operating system. For a path on the current platform, start from a known absolute base such as Path.home() or Path.cwd(), then join the remaining components. With os.path, os.path.join(base, "reports", "summary.csv") combines components, while os.path.abspath(relative_path) resolves a relative path against the process’s current working directory. Neither operation verifies that the target exists.
Work with Relative Paths
A relative path names a location from a base directory rather than from a filesystem root. For example, Path("data") / "input.csv" remains relative. When a program uses such a path for filesystem access, the operating system interprets it relative to the process’s current working directory unless the program explicitly supplies another base. That directory may differ from the directory containing the Python script, so choose a base that matches how the program is launched.
For paths relative to a known directory, build that base explicitly, then append components: project_dir / "data" / "input.csv". The equivalent os.path.join(project_dir, "data", "input.csv") works with string paths. These operations only construct path values; use file APIs separately to read, write, or check files.