When your Python script needs to read data from a file or write results back, it first needs to know where that file is located on your computer's storage (like a hard drive or SSD). Just like you need an address to find a specific house, your program needs a file path to find a specific file. This path is essentially the file's address within the hierarchical structure of folders (also called directories) on your system.
Think of your computer's file system as a large filing cabinet. The main cabinet is the root, drawers are main folders or drives, folders inside drawers are sub-folders, and finally, the documents inside are the files. A file path tells Python exactly which drawer, which folder within that drawer, and which document (file) to access.
A typical file path consists of:
.txt
, .csv
, .py
).The exact format, especially the separator character, depends on your operating system.
File paths are represented slightly differently depending on whether you are using Windows, macOS, or Linux.
Windows: Paths often start with a drive letter (like C:
or D:
), followed by a colon. Directories are separated by a backslash (\
).
C:\Users\YourUsername\Documents\report.txt
/
) in paths as well, which can make cross-platform code easier to write.macOS and Linux (Unix-like systems): These systems use a single root directory, represented by a forward slash (/
). Directories are always separated by a forward slash (/
). There are no drive letters like in Windows.
/Users/yourusername/Documents/report.txt
/home/yourusername/documents/report.txt
Understanding these differences is important, especially if you plan to share your scripts with others who might use a different operating system.
An absolute path provides the complete location of a file or directory starting from the very top level of the file system.
C:\...
)./...
).Examples:
C:\Program Files\Python310\python.exe
/Applications/Calculator.app
/usr/bin/python3
Absolute paths are unambiguous; they point to one specific location regardless of where your script is currently running. However, hardcoding absolute paths into your scripts can make them less portable. If you move your project to a different location or share it with someone else, the absolute path might no longer be correct.
A relative path specifies the location of a file or directory relative to the current working directory (CWD). The CWD is the directory from which your Python script is executed.
Think of it like giving directions from where you are currently standing: "go into the data
folder and find results.csv
" or "go up one level and then into the config
folder".
Special symbols are used in relative paths:
.
(a single dot): Represents the current directory...
(two dots): Represents the parent directory (one level up).Examples (assuming your script is running from /Users/alice/project/
on macOS/Linux or C:\Users\Alice\project\
on Windows):
data.txt
: Looks for data.txt
inside the CWD (project
folder).input/config.json
: Looks for a folder named input
inside the CWD, and then for config.json
inside that input
folder. Full path would be /Users/alice/project/input/config.json
.../scripts/utility.py
: Looks for the parent directory of the CWD (which is /Users/alice/
or C:\Users\Alice\
), then looks for a scripts
folder inside that parent directory, and finally for utility.py
inside scripts
. Full path would be /Users/alice/scripts/utility.py
.Example file structure illustrating relative and absolute paths from the
project
directory, which is the Current Working Directory (CWD).
Relative paths make your projects more self-contained and portable. If you organize your data files within your project directory, you can use relative paths, and the script will still work even if the entire project folder is moved elsewhere.
You can find out the CWD of your running script using Python's built-in os
module:
import os
current_directory = os.getcwd()
print(f"The current working directory is: {current_directory}")
Running this snippet will show you the absolute path to the directory your terminal or IDE considers the starting point for executing your script.
For beginners, and for most project work, using relative paths is generally recommended. Organize your project with subdirectories for data, scripts, etc., and use relative paths like data/my_file.txt
or ../config/settings.ini
. This makes your code easier to share and run on different computers.
Use absolute paths primarily when you need to access a file in a fixed, known location outside your project's structure, such as system configuration files or resources shared across different projects. Be mindful that this can tie your script to a specific machine setup.
Python's file handling functions (like open()
, which we'll see next) and standard library modules like os.path
and the newer pathlib
provide tools to work with both types of paths and help manage differences between operating systems. For now, the important part is understanding the concept of how your computer, and therefore Python, locates the files you want to work with.
© 2025 ApX Machine Learning