Python Pathlib Tutorial

Python has inbuilt standard libraries  and other packages for navigating the file system. Among them include os, shutil and glob. In this tutorial we will be looking at Pathlib a nice library that utilizes an object oriented approach for working with the file system.

The Pathlib package offers classes representing filesystem paths with semantics appropriate for different operating systems.

Let us see how to work with pathlib by comparing it with how  we use os.path.

Installation

For Python2 you will need to install it as

pip install pathlib2

Since Python3.4 ,pathlib is now part of the python standard library, hence there is no need to install it.

Let us start

# Load Pathlib
import pathlib
In [91]:
# Check All Methods of Pathlib
dir(pathlib)
Out[91]:
['EBADF',
 'EINVAL',
 'ELOOP',
 'ENOENT',
 'ENOTDIR',
 'Path',
 'PosixPath',
 'PurePath',
 'PurePosixPath',
 'PureWindowsPath',
 'S_ISBLK',
 'S_ISCHR',
 'S_ISDIR',
 'S_ISFIFO',
 'S_ISLNK',
 'S_ISREG',
 'S_ISSOCK',
 'Sequence',
 'WindowsPath',
 '_Accessor',
 '_Flavour',
 '_IGNORED_ERROS',
 '_IGNORED_WINERRORS',
 '_NormalAccessor',
 '_PathParents',
 '_PosixFlavour',
 '_PreciseSelector',
 '_RecursiveWildcardSelector',
 '_Selector',
 '_TerminatingSelector',
 '_WildcardSelector',
 '_WindowsFlavour',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_ignore_error',
 '_is_wildcard_pattern',
 '_make_selector',
 '_normal_accessor',
 '_posix_flavour',
 '_windows_flavour',
 'attrgetter',
 'fnmatch',
 'functools',
 'io',
 'nt',
 'ntpath',
 'os',
 'posixpath',
 're',
 'supports_symlinks',
 'sys',
 'urlquote_from_bytes']
In [92]:
# Load Os
import os
In [93]:
# Check All Methods of Pathlib
dir(os.path)
Out[93]:
['__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '_get_sep',
 '_joinrealpath',
 '_varprog',
 '_varprogb',
 'abspath',
 'altsep',
 'basename',
 'commonpath',
 'commonprefix',
 'curdir',
 'defpath',
 'devnull',
 'dirname',
 'exists',
 'expanduser',
 'expandvars',
 'extsep',
 'genericpath',
 'getatime',
 'getctime',
 'getmtime',
 'getsize',
 'isabs',
 'isdir',
 'isfile',
 'islink',
 'ismount',
 'join',
 'lexists',
 'normcase',
 'normpath',
 'os',
 'pardir',
 'pathsep',
 'realpath',
 'relpath',
 'samefile',
 'sameopenfile',
 'samestat',
 'sep',
 'split',
 'splitdrive',
 'splitext',
 'stat',
 'supports_unicode_filenames',
 'sys']
In [94]:
# Get Current Directory Using Os
os.getcwd()
Out[94]:
'/home/rooot/Documents/JLabs/PathTut'
In [95]:
# Get Current Directory Using Path
pathlib.Path.cwd()
Out[95]:
PosixPath('/home/rooot/Documents/JLabs/PathTut')
In [96]:
# Simple way
from pathlib import Path
In [97]:
Path.cwd()
Out[97]:
PosixPath('/home/rooot/Documents/JLabs/PathTut')
In [98]:
# Creating A Path From String with os.path
os.path.join('testfolder','example.txt')
Out[98]:
'testfolder/example.txt'
In [99]:
# Creating A Path From String with Path
Path('testfolder/example.txt')
Out[99]:
PosixPath('testfolder/example.txt')
In [100]:
# Method 2 Using Forward Slash Operator To Join Path
Path('testfolder') / 'example.txt'
Out[100]:
PosixPath('testfolder/example.txt')
In [101]:
#  Using Forward Slash Operator To Join Full Path
Path.home() / 'testfolder' / 'example.txt'
Out[101]:
PosixPath('/home/rooot/testfolder/example.txt')
In [102]:
# Method 3 Using JoinPath
Path().joinpath('testfolder', 'example.txt')
Out[102]:
PosixPath('testfolder/example.txt')
In [103]:
#Using JoinPath For Full Path
Path.home().joinpath('testfolder', 'example.txt')
Out[103]:
PosixPath('/home/rooot/testfolder/example.txt')
In [104]:
# How to Get the Home Directory with Os.path
os.path.expanduser("~")
Out[104]:
'/home/rooot'
In [105]:
# How to Get the Home Directory with Os
os.environ['HOME']
Out[105]:
'/home/rooot'
In [106]:
# How to Get the Home Directory with Path
Path.home()
Out[106]:
PosixPath('/home/rooot')
In [110]:
# How to Get the Home Directory with Path
Path('~').expanduser()
Out[110]:
PosixPath('/home/rooot')
In [111]:
# How to Get Absolute Path with OS
os.path.abspath('testfolder/example.txt')
Out[111]:
'/home/rooot/Documents/JLabs/PathTut/testfolder/example.txt'
In [112]:
# How to Get Absolute Path with Path
Path('testfolder/example.txt').resolve()
Out[112]:
PosixPath('/home/rooot/Documents/JLabs/PathTut/testfolder/example.txt')
In [113]:
# How to Get Absolute Path with Path
Path('testfolder/example.txt').absolute()
Out[113]:
PosixPath('/home/rooot/Documents/JLabs/PathTut/testfolder/example.txt')

Getting The Components of a Path

In [114]:
os_path = os.path.join('testfolder','example.txt')
p_path = Path('testfolder') / 'example.txt'
In [115]:
dir(os_path)
Out[115]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getnewargs__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mod__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rmod__',
 '__rmul__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'capitalize',
 'casefold',
 'center',
 'count',
 'encode',
 'endswith',
 'expandtabs',
 'find',
 'format',
 'format_map',
 'index',
 'isalnum',
 'isalpha',
 'isascii',
 'isdecimal',
 'isdigit',
 'isidentifier',
 'islower',
 'isnumeric',
 'isprintable',
 'isspace',
 'istitle',
 'isupper',
 'join',
 'ljust',
 'lower',
 'lstrip',
 'maketrans',
 'partition',
 'replace',
 'rfind',
 'rindex',
 'rjust',
 'rpartition',
 'rsplit',
 'rstrip',
 'split',
 'splitlines',
 'startswith',
 'strip',
 'swapcase',
 'title',
 'translate',
 'upper',
 'zfill']
In [116]:
dir(p_path)
Out[116]:
['__bytes__',
 '__class__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__enter__',
 '__eq__',
 '__exit__',
 '__format__',
 '__fspath__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rtruediv__',
 '__setattr__',
 '__sizeof__',
 '__slots__',
 '__str__',
 '__subclasshook__',
 '__truediv__',
 '_accessor',
 '_cached_cparts',
 '_closed',
 '_cparts',
 '_drv',
 '_flavour',
 '_format_parsed_parts',
 '_from_parsed_parts',
 '_from_parts',
 '_hash',
 '_init',
 '_make_child',
 '_make_child_relpath',
 '_opener',
 '_parse_args',
 '_parts',
 '_pparts',
 '_raise_closed',
 '_raw_open',
 '_root',
 '_str',
 'absolute',
 'anchor',
 'as_posix',
 'as_uri',
 'chmod',
 'cwd',
 'drive',
 'exists',
 'expanduser',
 'glob',
 'group',
 'home',
 'is_absolute',
 'is_block_device',
 'is_char_device',
 'is_dir',
 'is_fifo',
 'is_file',
 'is_mount',
 'is_reserved',
 'is_socket',
 'is_symlink',
 'iterdir',
 'joinpath',
 'lchmod',
 'lstat',
 'match',
 'mkdir',
 'name',
 'open',
 'owner',
 'parent',
 'parents',
 'parts',
 'read_bytes',
 'read_text',
 'relative_to',
 'rename',
 'replace',
 'resolve',
 'rglob',
 'rmdir',
 'root',
 'samefile',
 'stat',
 'stem',
 'suffix',
 'suffixes',
 'symlink_to',
 'touch',
 'unlink',
 'with_name',
 'with_suffix',
 'write_bytes',
 'write_text']
In [117]:
# Get Name of File with Path
p_path.name
Out[117]:
'example.txt'
In [118]:
# Get Name of File with Os
# os.path.basename('testfolder/example.txt') 
os.path.basename(os_path)
Out[118]:
'example.txt'
In [119]:
# Get Directory Name
p_path.parent
Out[119]:
PosixPath('testfolder')
In [120]:
# Get Directory Name
os.path.dirname(os_path)
Out[120]:
'testfolder'
In [122]:
# Get the File Extension/Suffix with OS
os.path.splitext(os_path)[1]
Out[122]:
'.txt'
In [123]:
# Get the File Extension/Suffix with Path
p_path.suffix
Out[123]:
'.txt'
In [124]:
# Get the File Stem with OS
os.path.splitext(os_path)[0]
Out[124]:
'testfolder/example'
In [125]:
# Get the File Stem with Path
p_path.stem
Out[125]:
'example'

Making Directory and Files

  • os.mkdir()
  • os.makedirs()
In [126]:
# Make Directory with Os
os.mkdir('folder001')
In [127]:
# Make directory with Path
Path('folder002').mkdir()
In [128]:
# Create A File If Folder Exist
Path('folder002').mkdir(exist_ok=True)
In [129]:
# Create A File inside folder
Path('folder002/newfile.txt').touch()
In [ ]:

In [ ]:

Looping Through Subdirectories

In [130]:
import glob
In [131]:
all_dir = glob.glob('testfolder/*.txt')
In [132]:
all_dir
Out[132]:
['testfolder/addfiles9.txt',
 'testfolder/file1.txt',
 'testfolder/file9.txt',
 'testfolder/file4.txt',
 'testfolder/file7.txt',
 'testfolder/file2.txt',
 'testfolder/file8.txt',
 'testfolder/addfiles10.txt',
 'testfolder/addfiles0.txt',
 'testfolder/file5.txt',
 'testfolder/file6.txt',
 'testfolder/addfiles2.txt',
 'testfolder/addfiles1.txt',
 'testfolder/addfiles4.txt',
 'testfolder/file10.txt',
 'testfolder/addfiles6.txt',
 'testfolder/addfiles7.txt',
 'testfolder/file3.txt',
 'testfolder/addfiles3.txt',
 'testfolder/addfiles8.txt',
 'testfolder/addfiles5.txt',
 'testfolder/file0.txt']
In [133]:
for file in all_dir:
    print(file)
testfolder/addfiles9.txt
testfolder/file1.txt
testfolder/file9.txt
testfolder/file4.txt
testfolder/file7.txt
testfolder/file2.txt
testfolder/file8.txt
testfolder/addfiles10.txt
testfolder/addfiles0.txt
testfolder/file5.txt
testfolder/file6.txt
testfolder/addfiles2.txt
testfolder/addfiles1.txt
testfolder/addfiles4.txt
testfolder/file10.txt
testfolder/addfiles6.txt
testfolder/addfiles7.txt
testfolder/file3.txt
testfolder/addfiles3.txt
testfolder/addfiles8.txt
testfolder/addfiles5.txt
testfolder/file0.txt
In [134]:
# Using Path
p = Path('testfolder')
In [135]:
for file in p.glob('*.txt'):
    print(file)
testfolder/addfiles9.txt
testfolder/file1.txt
testfolder/file9.txt
testfolder/file4.txt
testfolder/file7.txt
testfolder/file2.txt
testfolder/file8.txt
testfolder/addfiles10.txt
testfolder/addfiles0.txt
testfolder/file5.txt
testfolder/file6.txt
testfolder/addfiles2.txt
testfolder/addfiles1.txt
testfolder/addfiles4.txt
testfolder/file10.txt
testfolder/addfiles6.txt
testfolder/addfiles7.txt
testfolder/file3.txt
testfolder/addfiles3.txt
testfolder/addfiles8.txt
testfolder/addfiles5.txt
testfolder/file0.txt
In [136]:
# Checking Subdirectory traversely
# Method 1 with **/
all_sub_dir = Path('testfolder').glob('**/*.txt')
In [137]:
# Show all Folders/Sub folders and their files
list(all_sub_dir)
Out[137]:
[PosixPath('testfolder/addfiles9.txt'),
 PosixPath('testfolder/file1.txt'),
 PosixPath('testfolder/file9.txt'),
 PosixPath('testfolder/file4.txt'),
 PosixPath('testfolder/file7.txt'),
 PosixPath('testfolder/file2.txt'),
 PosixPath('testfolder/file8.txt'),
 PosixPath('testfolder/addfiles10.txt'),
 PosixPath('testfolder/addfiles0.txt'),
 PosixPath('testfolder/file5.txt'),
 PosixPath('testfolder/file6.txt'),
 PosixPath('testfolder/addfiles2.txt'),
 PosixPath('testfolder/addfiles1.txt'),
 PosixPath('testfolder/addfiles4.txt'),
 PosixPath('testfolder/file10.txt'),
 PosixPath('testfolder/addfiles6.txt'),
 PosixPath('testfolder/addfiles7.txt'),
 PosixPath('testfolder/file3.txt'),
 PosixPath('testfolder/addfiles3.txt'),
 PosixPath('testfolder/addfiles8.txt'),
 PosixPath('testfolder/addfiles5.txt'),
 PosixPath('testfolder/file0.txt'),
 PosixPath('testfolder/newfolder2/exfile0.txt'),
 PosixPath('testfolder/newfolder2/exfile7.txt'),
 PosixPath('testfolder/newfolder2/exfile3.txt'),
 PosixPath('testfolder/newfolder2/exfile4.txt'),
 PosixPath('testfolder/newfolder2/movedexample003.txt'),
 PosixPath('testfolder/newfolder2/exfile9.txt'),
 PosixPath('testfolder/newfolder2/exfile8.txt'),
 PosixPath('testfolder/newfolder2/exfile10.txt'),
 PosixPath('testfolder/newfolder2/movedexample002.txt'),
 PosixPath('testfolder/newfolder2/exfile5.txt'),
 PosixPath('testfolder/newfolder2/movedexample001.txt'),
 PosixPath('testfolder/newfolder2/exfile1.txt'),
 PosixPath('testfolder/newfolder2/exfile6.txt'),
 PosixPath('testfolder/newfolder2/exfile2.txt')]
In [138]:
# Checking Subdirectory traversely
# Method 2 with rglob (recursive glob)
all_sub_dir2 = Path('testfolder').rglob('*.txt')
In [139]:
# Show all Folders/Sub folders and their files
list(all_sub_dir2)
Out[139]:
[PosixPath('testfolder/addfiles9.txt'),
 PosixPath('testfolder/file1.txt'),
 PosixPath('testfolder/file9.txt'),
 PosixPath('testfolder/file4.txt'),
 PosixPath('testfolder/file7.txt'),
 PosixPath('testfolder/file2.txt'),
 PosixPath('testfolder/file8.txt'),
 PosixPath('testfolder/addfiles10.txt'),
 PosixPath('testfolder/addfiles0.txt'),
 PosixPath('testfolder/file5.txt'),
 PosixPath('testfolder/file6.txt'),
 PosixPath('testfolder/addfiles2.txt'),
 PosixPath('testfolder/addfiles1.txt'),
 PosixPath('testfolder/addfiles4.txt'),
 PosixPath('testfolder/file10.txt'),
 PosixPath('testfolder/addfiles6.txt'),
 PosixPath('testfolder/addfiles7.txt'),
 PosixPath('testfolder/file3.txt'),
 PosixPath('testfolder/addfiles3.txt'),
 PosixPath('testfolder/addfiles8.txt'),
 PosixPath('testfolder/addfiles5.txt'),
 PosixPath('testfolder/file0.txt'),
 PosixPath('testfolder/newfolder2/exfile0.txt'),
 PosixPath('testfolder/newfolder2/exfile7.txt'),
 PosixPath('testfolder/newfolder2/exfile3.txt'),
 PosixPath('testfolder/newfolder2/exfile4.txt'),
 PosixPath('testfolder/newfolder2/movedexample003.txt'),
 PosixPath('testfolder/newfolder2/exfile9.txt'),
 PosixPath('testfolder/newfolder2/exfile8.txt'),
 PosixPath('testfolder/newfolder2/exfile10.txt'),
 PosixPath('testfolder/newfolder2/movedexample002.txt'),
 PosixPath('testfolder/newfolder2/exfile5.txt'),
 PosixPath('testfolder/newfolder2/movedexample001.txt'),
 PosixPath('testfolder/newfolder2/exfile1.txt'),
 PosixPath('testfolder/newfolder2/exfile6.txt'),
 PosixPath('testfolder/newfolder2/exfile2.txt')]
In [140]:
# Using os.walk to get all files
for root,subdir,files in os.walk(os.path.join('testfolder')):
    for f in files:
        if f.endswith(".txt"):
            print(f)
addfiles9.txt
file1.txt
file9.txt
file4.txt
file7.txt
file2.txt
file8.txt
addfiles10.txt
addfiles0.txt
file5.txt
file6.txt
addfiles2.txt
addfiles1.txt
addfiles4.txt
file10.txt
addfiles6.txt
addfiles7.txt
file3.txt
addfiles3.txt
addfiles8.txt
addfiles5.txt
file0.txt
exfile0.txt
exfile7.txt
exfile3.txt
exfile4.txt
movedexample003.txt
exfile9.txt
exfile8.txt
exfile10.txt
movedexample002.txt
exfile5.txt
movedexample001.txt
exfile1.txt
exfile6.txt
exfile2.txt
In [141]:
# Using os.walk to get all subdirectory
for root,subdir,files in os.walk(os.path.join('testfolder')):
    for s in subdir:
        print(s)
newfolder2

Checking Files and Directory and Path

  • exists
  • is_dir()/isdir()
  • is_file()/isfile()
In [142]:
print(os_path)
print(p_path)
testfolder/example.txt
testfolder/example.txt
In [143]:
# Check if it is a directory
os.path.isdir(os_path)
Out[143]:
False
In [144]:
# Check if it is a directory
os.path.isdir(os.path.join('testfolder'))
Out[144]:
True
In [145]:
# Check if it is a directory
p_path.is_dir()
Out[145]:
False
In [146]:
p_path
Out[146]:
PosixPath('testfolder/example.txt')
In [147]:
# Parent Folder is a Directory
p_path.parent.is_dir()
Out[147]:
True
In [148]:
# Check if it is a file with Os
os.path.isfile('testfolder/example.txt')
Out[148]:
False
In [149]:
# Check if it is a file
p_path.is_file()
Out[149]:
False
In [152]:
Path("example.txt").is_file()
Out[152]:
True
In [153]:
for file in p.glob('*.txt'):
    print(file.is_file())
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
True
In [ ]:
# Moving Files
+ shutil.move()
+ os.replace()
+ Path().replace()
In [154]:
import shutil
In [156]:
shutil.move('testfolder/example001.txt','testfolder/newfolder2/movedexample001.txt')
Out[156]:
'testfolder/newfolder2/movedexample001.txt'
In [157]:
os.replace('testfolder/example002.txt','testfolder/newfolder2/movedexample002.txt')
In [158]:
# using path
Path('testfolder/example003.txt').replace('testfolder/newfolder2/movedexample003.txt')

File Stats

  • os.stat()
  • Path.stat()
In [159]:
os.stat('example.txt')
Out[159]:
os.stat_result(st_mode=33188, st_ino=5650645, st_dev=2055, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1582546033, st_mtime=1582472864, st_ctime=1582472864)
In [160]:
# Using Path
Path('example.txt').stat()
Out[160]:
os.stat_result(st_mode=33188, st_ino=5650645, st_dev=2055, st_nlink=1, st_uid=1000, st_gid=1000, st_size=0, st_atime=1582546033, st_mtime=1582472864, st_ctime=1582472864)
In [161]:
# Owner of File
Path('example.txt').owner()
Out[161]:
'rooot'
In [162]:
# Owner/Group of File
Path('example.txt').group()
Out[162]:
'rooot'
In [165]:
# Owner of File
os.stat('example.txt').st_uid
Out[165]:
1000
In [164]:
# Owner/Group of File
os.stat('example.txt').st_gid
Out[164]:
1000
In [166]:
import pwd
pwd.getpwuid(os.stat('example.txt').st_gid)
Out[166]:
pwd.struct_passwd(pw_name='rooot', pw_passwd='x', pw_uid=1000, pw_gid=1000, pw_gecos='rooot,,,', pw_dir='/home/rooot', pw_shell='/bin/bash')

 

There are several things we can do with pathlib but these are some of them.

You can check out the entire video tutorial on our youtube channel or below

Thanks for your time

Jesus Saves

By Jesse E.Agbe(JCharis)

Leave a Comment

Your email address will not be published. Required fields are marked *