1.2. Paths in Python¶
On Windows, the backslash (\
) separates folders and filenames, but
on macOS and Linux, the forward slash ( /
) separates them. Instead
of writing code both ways to make your Python scripts cross-platform
compatible, you can use the pathlib
module and /
operator
instead.
The typical way to import pathlib is with the statement
from pathlib import Path
. Because the Path
class is the most
frequently used class in pathlib , this form lets you type Path instead
of pathlib.Path
. You can pass a string of a folder or filename to
Path()
to create a Path object of that folder or filename. As long
as the leftmost object in an expression is a Path object, you can use
the /
operator to join together Path
objects or strings. Enter
the following into the interactive shell:
>>> from pathlib import Path
>>> Path('spam') / 'bacon' / 'eggs'
PosixPath('spam/bacon/eggs')
>>> Path('spam') / Path('bacon/eggs')
PosixPath('spam/bacon/eggs')
>>> Path('spam') / Path('bacon', 'eggs')
PosixPath('spam/bacon/eggs')
Note that because I ran this code on a Windows machine, Path()
returns WindowsPath objects. On macOS and Linux, a PosixPath object is
returned. For our purposes, there’s no difference between these two
types.
POSIX is a set of standards for Unix-like operating systems and is beyond the scope of this book.
You can pass a Path object to any function in the Python standard
library that expects a filename. For example, the function call
open(Path('C:\\') / 'Users' / 'Al' / 'Desktop' / 'spam.py')
is
equivalent to open(r'C:\Users\Al\Desktop\spam.py')
.
1.2.1. The Home Directory¶
All users have a folder called the home folder or home directory for
their own files on the computer. You can get a Path object of the home
folder by calling Path.home()
:
>>> Path.home()
PosixPath('/home/bk')
The home directories are located in a set place depending on your operating system:
On Windows, home directories are in
C:\Users
.On Mac, home directories are in
/Users
.On Linux, home directories are often in
/home
.
Your scripts will almost certainly have permissions to read from and write to the files in your home directory, so it’s an ideal place to store the files that your Python programs will work with.
1.2.2. The Current Working Directory¶
Every program that runs on your computer has a current working directory (cwd). Any filenames or paths that don’t begin with the root folder you can assume are in the cwd. Although “folder” is the more modern name for a Environment Setup and the Command Line directory, note that cwd (or just working directory) is the standard term, not “current working folder” .
You can get the cwd as a Path object using the Path.cwd()
function
and change it using os.chdir()
. Enter the following into the
interactive shell:
>>> from pathlib import Path
>>> import os
>>> Path.cwd()
PosixPath('/home/bk/book-jubook/python/jubook_python/pt12_Beyond-the-Basic-Stuff/ch02_ENVIRONMENT-SE-TUP-AND-THE-COMMAND-LINE')
>>> os.chdir('/tmp')
>>> Path.cwd()
PosixPath('/tmp')
Here, the cwd was set to /tmp
, so the filename project.docx
would refer to /tmp/project.docx
. When we change the cwd to
C:\Windows\System32
, the filename project.docx
would refer to
C:\Windows\System32\project.docx
.
Python displays an error if you try to change to a directory that doesn’t exist:
>>> os.chdir('C:/ThisFolderDoesNotExist')
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Cell In[11], line 1
----> 1 os.chdir('C:/ThisFolderDoesNotExist')
FileNotFoundError: [Errno 2] No such file or directory: 'C:/ThisFolderDoesNotExist'
The os.getcwd()
function in the os module is a former way of getting
the cwd as a string.
1.2.3. Absolute vs. Relative Paths¶
There are two ways to specify a file path:
An absolute path, which always begins with the root folder
A relative path, which is relative to the program’s cwd
There are also the dot ( .
) and dot-dot ( ..
) folders. These
are not real folders but special names that you can use in a path. A
single period ( .
) for a folder name is shorthand for “this
directory”. Two periods ( ..
) means “the parent folder” .
Figure below shows an example of some folders and files. When the cwd is
set to C:\bacon
, the relative paths for the other folders and files
are set as they are in the figure.
The .\
at the start of a relative path is optional. For example,
.\spam.txt
and spam.txt
refer to the same file.
>>>