Python’s pathlib - usage examples

This documenation presents basic usage examples of Python’s pathlib library. Before trying examples in this page make sure to check your Python version by reading the below note or visit here to check.

Pathlib-inheritance

What it offers, what speciality it has?

pathlib offers a set of classes to handle filesystem paths. It offers the following advantages over using string objects:

pathlib inheritance
pathlib inheritance

Note

» Mean to say

Python 2.6 - Python3.3 >= Python 3.4
pip install pathlib OR No installation is required just try it
easy_install pathlib Now it’s the part of Python’s standard library

Mine is Python 3.7.2 and I am trying it on MAC OS Mojave.

pathlib

Directory structure of working directory

We will be working on root directory. This directory has the following structure.

Rishikeshs-MacBook-Air:try-pathlib hygull$ pwd
/Users/hygull/Projects/Python3/try-pathlib
Rishikeshs-MacBook-Air:try-pathlib hygull$ tree root/

root/
├── c
│   └── examples
│       ├── c-main.md
│       └── hello.c
├── cpp
│   ├── docs
│   │   └── notes.md
│   └── hello.cpp
├── doc.md
├── go
│   ├── docs
│   │   ├── links.md
│   │   └── loop.py
│   ├── hello.go
│   └── images
│       ├── go-slices-usage-and-internals_slice-2.png
│       ├── go.jpeg
│       ├── rishikesh.jpeg
│       └── rishikesh.png
├── js
│   ├── hello.js
│   └── try
│       └── examples
│           └── dict-example.py
├── main.md
└── python
    ├── examples
    │   └── go
    │       └── slice.go
    ├── hello.py
    └── images
        ├── python.jpeg
        └── rishikesh.webp

Getting started

Now, let’s move/navigate to root directory which is to be assumed as the working directory in this documentation.

Rishikeshs-MacBook-Air:try-pathlib hygull$ cd root/
Rishikeshs-MacBook-Air:root hygull$ ls
c	cpp	doc.md	go	js	main.md	python
Rishikeshs-MacBook-Air:root hygull$

Rishikeshs-MacBook-Air:root hygull$ python3
Python 3.7.2 (default, Jan 13 2019, 12:50:01) 
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

We are done, let’s start.

try-pathlib-mac

» Listing out directories and files of current directory separately

>>> from pathlib import Path
>>> 
>>> root = Path(".")
>>> root
PosixPath('.')
>>> 
>>> directories = [dir_content for dir_content in root.iterdir() if dir_content.is_dir()]
>>> directories
[PosixPath('go'), PosixPath('python'), PosixPath('js'), PosixPath('cpp'), PosixPath('c')]
>>>
>>> files = [dir_content for dir_content in root.iterdir() if dir_content.is_file()]
>>> files
[PosixPath('doc.md'), PosixPath('main.md')]
>>> 

» Getting absoute path of root directory

>>> root.absolute()
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root')
>>> 

» Getting home directory’s absolute path

>>> root.home()
PosixPath('/Users/hygull')

» Listing out all python files present in any of the directories availble under current directory

>>> list(root.glob("**/*.py"))
[PosixPath('go/docs/loop.py'), PosixPath('python/hello.py'), PosixPath('js/try/examples/dict-example.py')]
>>> 


» Navigating to root/python/examples/go and lisiting out its content(s).

Rishikeshs-MacBook-Air:root hygull$ 
Rishikeshs-MacBook-Air:root hygull$ ls python/examples/
go
Rishikeshs-MacBook-Air:root hygull$ ls python/examples/go/
slice.go
Rishikeshs-MacBook-Air:root hygull$ cat python/examples/go/slice.go 
package main 

import "fmt"

func main() {
	a := []int{12, 5, 6, 8}
	fmt.Print(a)
}

>>> root
PosixPath('.')
>>> 
>>> python = root / "python"
>>> python
PosixPath('python')
>>> 
>>> python.exists()
True
>>> 
>>> examples = python / "examples"
>>> examples
PosixPath('python/examples')
>>> 
>>> examples.exists()
True
>>> 
>>> go = examples / "go"
>>> go
PosixPath('python/examples/go')
>>> 
>>> go.exists()
True
>>> 

>>> list(go.iterdir())
[PosixPath('python/examples/go/slice.go')]
>>> 
>>> examples = root / "examples"
>>> examples.exists()
False
>>> 
>>> examples = root / "python" / "examples"
>>> examples.exists()
True
>>> 
>>> go = root / "python" / "examples" / "go"
>>> go.exists()
True
>>> 

» Getting URI

>>> python
PosixPath('python')
>>> 
>>> python.as_uri()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pathlib.py", line 714, in as_uri
    raise ValueError("relative path can't be expressed as a file URI")
ValueError: relative path can't be expressed as a file URI
>>> 
>>> python = python.resolve()
>>> python
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root/python')
>>> 
>>> python.as_uri()
'file:///Users/hygull/Projects/Python3/try-pathlib/root/python'
>>> 

» Printing parent directories and checking their existence

>>> go = go.resolve()
>>> go.as_uri()
'file:///Users/hygull/Projects/Python3/try-pathlib/root/python/examples/go'
>>>

>>> examples = go.parent
>>> examples
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root/python/examples')
>>> 
>>> examples.exists()
True
>>> 

>>> python = examples.parent
>>> python
PosixPath('/Users/hygull/Projects/Python3/try-pathlib/root/python')
>>> 
>>> python.exists()
True
>>> 

» Creating a directory with a file inside it with few lines

The output of the below code is created_files directory with message.txt file inside it.

"""
    {
        "date_created": "2 march 2019, Sat",
        "aim": "Creating files with contents (few lines)",
        "created_by": "Rishikesh Agrawani",
        "description": "First the code will check if there is a directory 
                        named `created_files` in the current working directory
                        if it exists, it will create files inside that
                        it it doesn't exist, create the folder first and then create file
                        named `message.txt` inside that"

    }
"""

from pathlib import Path 


cwd = Path(".")
created_files_dir = cwd / "created_files"

if not created_files_dir.exists():
    created_files_dir.mkdir()

file = created_files_dir / "message.txt"

lines = [
    "It is better to learn programming for solving complex problems.",
    "Python, Java, Go, C, JavaScript, Ruby, PHP are popular.",
    "So finally, we all are in a right place"
]

file.write_text('\n'.join(lines))

» Creating directories in the current working directory

"""
    {
        "date_created": "4 march 2019, Mon",
        "aim": "Creating directories whose names are mentioned in a text file",
        "created_by": "Rishikesh Agrawani",
        "description": "First program will check if directory named `created_directories` exists or not
                       If it does exist it creates that and after that it reads a text file 
                       `../docs/texts/directory_names.txt` to read list of directory names and create 
                       those directories inside `created_directories` directory.
                       
                       Here I have not coded for validations etc. so assume that I have valid names in 
                       the text file (check it)"
    }
"""

from pathlib import Path 


cwd = Path(".").resolve()
created_directories_dir_path = cwd / "created_directories"

if not created_directories_dir_path.exists():
    created_directories_dir_path.mkdir()
    print("Directory successfully created")

# Getting path of the file to be read
directory_names_file_path = cwd.parent / "docs" / "texts" / "directory_names.txt"

print(directory_names_file_path) 
print(directory_names_file_path.exists())
print(directory_names_file_path.is_file())

directory_names = directory_names_file_path.read_text().split('\n')

# ['Gayle', 'Sachin', 'Sehwag', 'Garry', 'Dhoni', 'Ricky', 'Adam', 'Ken', 'Dennis', 'Wes']
print(directory_names) 

for directory_name in directory_names:
    directory_path = created_directories_dir_path / directory_name.strip()
    directory_path.mkdir()
    print("Created", directory_path)

# Storing absolute path of all the newly created directories
new_dir_paths = [str(new_dir_path) for new_dir_path in created_directories_dir_path.iterdir() if new_dir_path.is_dir()]
print(new_dir_paths)
# ['/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sehwag', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dennis', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dhoni', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ricky', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Garry', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Gayle', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Wes', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sachin', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ken', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Adam']

Ouput

Rishikeshs-MacBook-Air:src hygull$ python3 create_directories.py 
Directory successfully created
/Users/hygull/Projects/Python3/try-pathlib/docs/texts/directory_names.txt
True
True
['Gayle', 'Sachin', 'Sehwag', 'Garry', 'Dhoni', 'Ricky', 'Adam', 'Ken', 'Dennis', 'Wes']
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Gayle
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sachin
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sehwag
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Garry
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dhoni
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ricky
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Adam
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ken
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dennis
Created /Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Wes
['/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sehwag', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dennis', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Dhoni', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ricky', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Garry', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Gayle', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Wes', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Sachin', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Ken', '/Users/hygull/Projects/Python3/try-pathlib/src/created_directories/Adam']

Help

>>> from pathlib import Path
>>> 
>>> root = Path(".")
>>> 
>>> dir(root)
['__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']
>>> 
>>> help(root)

>>> help(root.exists())
>>> 
>>> help(root.as_uri)

>>> 

Screenshots

ScreenShot2019-03-02at8.26.27AM.png

ScreenShot2019-03-02at9.31.17AM.png

ScreenShot2019-03-02at12.31.04PM.png

ScreenShot2019-03-02at2.25.17PM.png

ScreenShot2019-03-02at2.40.59PM.png

ScreenShot2019-03-02at3.20.25PM.png

ScreenShot2019-03-02at3.33.55PM.png

ScreenShot2019-03-02at3.47.52PM.png

References