Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
11.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
hello.txt in your home directory the path to that file is
/Users/yourname/hello.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
The History of Path Separators
Why is the path separator a
/ on Unix/Linux/MacOS systems and
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
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\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
\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
hello.py in a folder called
CS150 that is inside a folder called
PyCharmProjects under your home directory, then the full name for the file
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
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.
Specifying a sub-folder is easy – you simply specify the sub-folder’s 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. Folders in the diagram are displayed in bold type.
Using the example file hierarchy above, the program,
could access each of the data files using the following relative file paths:
Here’s the important rule to remember: If your file and your Python program are
in the same directory you can simply use 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
open('../myData/data3.txt', 'r'), or in an absolute file path like