Skip to main content

Section 10.2 Finding a File on your Disk

Opening a file requires that you, as a programmer, and Python agree about the location of the file on your disk. The way that files are located on disk is by their path You can think of the filename as the short name for a file, and the path as the full name. For example on a Mac if you save the file info.txt in your home directory the path to that file is /Users/yourname/info.txt On a Windows machine the path looks a bit different but the same principles are in use. For example on windows the path might be C:\Users\yourname\My Documents\info.txt

Note 10.2.1.

The History of Path Separators
Why is the path separator a / on Unix/Linux/MacOS systems and \ on Microsoft Windows systems? The concept of a hierarchy of folders was first developed in Unix. On a Unix command line a / is used to separate folder names in a file path and dashes are used to specify command line options, e.g., path/to/file/myfile -long -reverse.
On a Windows system the / character is used for command line options, so the designers of Windows decided to use the \ for separating folder names in a file path, e.g., path\to\file\myfile /long /reverse.
Using a \ to separate folder names in a path is problematic because the \ character is also used as an escape character for special characters, such as \n for a new line character.
Bottom line, we will always use the / character to separate folder names in a path, and even on Windows system the file path will work just fine.
You can access files in sub-folders, also called directories, under your home directory by adding a slash and the name of the folder. For example, if you had a file called calculate.py in a folder called CS150 that is inside a folder called Projects under your home directory, then the full name for the file calculate.py is /Users/yourname/Projects/CS150/calculate.py. This is called an absolute file path. An absolute file path typically only works on a specific computer. Think about it for a second. What other computer in the world is going to have an absolute file path that starts with /Users/yourname?
If a file is not in the same folder as your python program, you need to tell the computer how to reach it. A relative file path starts from the folder that contains your python program and follows a computer’s file hierarchy. A file hierarchy contains folders which contains files and other sub-folders.To specify a sub-folder, you specify its name. To specify a parent folder you use the special .. notation because every file and folder has one unique parent. You can use the .. notation multiple times in a file path to move multiple levels up a file hierarchy. Here is an example file hierarchy that contains multiple folders, files, and sub-folders.
Using the example file hierarchy above, the program python_program.py could access each of the data files using the following relative file paths:
  • data1.txt
  • ../my_data/data2.txt
  • ../my_data/data3.txt
  • ../../extra_data/data4.txt
Here’s the important rule to remember: If your file and your Python program are in the same directory, all you need to specify is the filename like this: open('myfile.txt', 'r'). If your file and your Python program are in different directories, then you must refer to one or more directories, either in a relative file path to the file like this: open('../my_data/data3.txt', 'r'), or in an absolute file path like open('/users/bmiller/my_files/all_projects/my_data/data3.txt', 'r').