The pathlib package#

Warning

None of the following code will work on your computer. You will need to provide your own unique paths for your machine.

path objects#

from pathlib import Path
desktop_path = Path('/Users/tyson/Desktop')
desktop_path
PosixPath('/Users/tyson/Desktop')

absolute and relative paths#

absolute_path = Path('/Users/tyson/Desktop')
absolute_path
PosixPath('/Users/tyson/Desktop')
relative_path = Path('./')
relative_path
PosixPath('.')
relative_path.is_absolute()
False
# this will create the full absolute path
relative_path_full = relative_path.resolve()
# this tells me where I am
current_dir = Path().cwd()

constructing paths#

desktop_path = Path('/Users/tyson/Desktop')
project_data_path = desktop_path / 'my_project' / 'data.csv'
project_data_path
PosixPath('/Users/tyson/Desktop/my_project/data.csv')

creating and deleting folders and files#

# create a folder
project_path = Path('/Users/tyson/Desktop/my_project')
project_path.mkdir()
# create a file
project_data_path = Path('/Users/tyson/Desktop/my_project/data.csv')
project_data_path.touch()
# delete a file
project_data_path.unlink()
# delete a folder
project_path.rmdir()

checking path properties#

project_path = absolute_path / 'my_project'
project_path.exists()
False
project_path.mkdir()
project_path.exists()
True
# only true if exists & is a directory
project_path.is_dir()
True
project_path.is_file()
False
# get the user’s home directory
project_path.home()
PosixPath('/Users/tyson')
parent_dir     = project_data_path.parent
file_name      = project_data_path.name
file_name_stem = project_data_path.stem
file_suffix    = project_data_path.suffix

print(parent_dir)
print(file_name)
print(file_name_stem)
print(file_suffix)
/Users/tyson/Desktop/my_project
data.csv
data
.csv

finding files and folders#

# set things up
desktop_path = Path('/Users/tyson/Desktop')
project_path = desktop_path / 'my_project'
project_data_path = Path('/Users/tyson/Desktop/my_project/data.csv')
project_data_path.touch()

# find all of the csv files in the "project_path" folder
list_of_csv_a = list( project_path.glob('*.csv') )
list_of_csv_a
[PosixPath('/Users/tyson/Desktop/my_project/data.csv')]
# recursively find all of the csv files in the "project_path" folder and subfolders
list_of_csv_b = list( project_path.rglob('*.csv') )
list_of_csv_b
[PosixPath('/Users/tyson/Desktop/my_project/data.csv')]
# clean up; delete the file and folder
project_data_path.unlink()
project_path.rmdir()