Python 3home |
Introduction to Python
davidbpython.com
Filepaths pinpoint the location of any file.
Your computer's filesystem contains files and folders, arranged in a tree (folders and files within folders within other folders, etc.) In this session we'll look at how we can open files anywhere on the filesystem tree. Here's a sample tree for us to work with, containing both files (ending in .txt) and python scripts (ending in .py). (This tree and files are replicated in your data folder for this session.)
dir1 ├── file1.txt ├── test1.py │ ├── dir2a │ ├── file2a.txt │ ├── test2a.py │ │ │ ├── dir3a │ │ ├── file3a.txt │ │ ├── test3a.py │ │ │ │ │ └── dir4 │ │ ├── file4.txt │ │ └── test4.py └── dir2b ├── file2b.txt ├── test2b.py │ └── dir3b ├── file3b.txt └── test3b.py
When our script is located in the same directory as a file we want to open, we can give Python the name of the file, and it will find it in this same directory.
""" test2b.py: open and read a file """
fh = open('file2b.txt') # OS looks for file in present working directory
print(fh.read()) # this is file 2b - note that it is in same directory as script
This works because test2b.py and file2b.txt are in the same directory.
However, if our script is in a different location from the file we want to open, we have a problem -- the OS won't be able to find the file.
""" test3a.py: open and read a file """
fh = open('file2b.txt') # raises a FileNotFoundError exception
# (OS looks for file in the pwd (dir3a)
# but doesn't find it)
The file exists, but it is in a different directory. The OS can't find the file because it needs to be told in which directory it should look for the file. So, if we are running our script from a different location than the file we wish to open, we must use a relative path or an absolute path to show the OS where the file is located.
There are two different ways of expressing a file's location.
Again, let's use the sample tree that can be found in your session folder:
dir1 ├── file1.txt ├── test1.py │ ├── dir2a │ ├── file2a.txt │ ├── test2a.py │ │ │ ├── dir3a │ │ ├── file3a.txt │ │ ├── test3a.py │ │ │ │ │ └── dir4 │ │ ├── file4.txt │ │ └── test4.py └── dir2b ├── file2b.txt ├── test2b.py │ └── dir3b ├── file3b.txt └── test3b.py
Absolute path: this is one that locates a file from the root of the filesystem. It lists each of the directories that lead from the root to the directory that holds the file.
In Windows, absolute paths begin with a drive letter, usually C:\:
""" test3a.py: open and read a file """
filepath = r'C:\Users\david\Downloads\python_data\session_03_strings_lists_files\dir1\dir2b\file2b.txt'
fh = open(filepath)
print(fh.read())
(Note that r'' should be used with any Windows paths that contain backslashes.)
On the Mac, absolute paths begin with a forward slash:
""" test3a.py: open and read a file """
filepath = '/Users/david/Downloads/python_data/session_03_strings_lists_files/dir1/dir2b/file2b.txt'
fh = open(filepath)
print(fh.read())
(The above paths assume that the python_data folder is in the Downloads directory; your may have placed yours elsewhere on your system. Of course, the above paths also assume that my home directory is called david/; yours is likely different.) Relative Path: locate a file folder in relation to the present working directory
A relative path is read as an extension of the present working directory. The below path assumes that our present working directory is /Users/david/Downloads/python_data/dir1/dir2a:
""" test2a.py: open and read a file """
filepath = 'dir3a/dir4/file4.txt' # starts from /Users/david/Downloads/python_data/dir1/dir2a
fh = open(filepath)
print(fh.read())
When we use a relative path, we can think of it as extending the pwd. So the whole path is: /Users/david/Downloads/python_data/dir1/dir2a/dir3a/dir4/file4.txt Therefore, in order to use a relative path, you must first ascertain your present working directory in the filesystem. Only then can you know the relative path needed to find the file you are looking for. Special Note: Windows paths and the "raw string" Note that Windows paths featuring backslashes should use r'' ("raw string"), in which a backslash is not seen as an escape sequence such as \n (newline). This is not required on Macs or on paths without backslashes. For simplicity, you can substitute forward slashes in Windows paths, and Python will translate the slashes for you. Using forward slashes is probably the easiest way to work with Windows paths in Python.
We use .. to signify the parent; this can be used in a relative filepath.
Again, let's use the sample tree that is replicated in your session folder:
dir1 ├── file1.txt ├── test1.py │ ├── dir2a │ ├── file2a.txt │ ├── test2a.py │ │ │ ├── dir3a │ │ ├── file3a.txt │ │ ├── test3a.py │ │ │ │ │ └── dir4 │ │ ├── file4.txt │ │ └── test4.py └── dir2b ├── file2b.txt ├── test2b.py │ └── dir3b ├── file3b.txt └── test3b.py
What if we are in dir3a running file test3a.py but want to access file file1.txt?
Think of .. (two dots) as representing the parent directory:
""" test3a.py: open and read a file """
filepath = '../../file1.txt' # reads from /Users/david/Downloads/python_data/dir1
fh = open(filepath)
print(fh.read())
As with all relative paths, you must first consider the location from which we are running the script, then the location of the file you're trying to open. If we are in the dir3 directory when we run test3a.py, then we are two directories "below" the dir1 directory. The first .. takes us to the dir2 directory. The second .. takes us to the dir1 directory. We can then access the file1.txt directory from there. Going up, then down What if we wanted to go from dir2a to dir2b? They are at the same level, in other words they are neither above or below each other.
The answer is to go up to the parent, then down to the other child:
""" test2a.py: open and read a file """
filepath = '../dir2b/file2b.txt'
fh = open(filepath)
print(fh.read())
.. takes us to the dir1 directory. dir2b can be accessed from that directory.