Python Module Tutorial - pathlib
Posted on Oct 19, 2020 in Python Programming - Beginner Level by Amo Chen ‐ 5 min read
Python’s os module provides many convenient functions for us to manipulate file/folder paths. After Python 3.4, a new module called pathlib was introduced, which encapsulates various file/folder related operations in classes such as Path
, making file/folder operations more object-oriented.
This article will explain and demonstrate the pathlib module.
Requirements
- Python 3.7
The Path Class
Under pathlib, there are many classes available for use, such as WindowsPath , PosixPath. Generally speaking, it is not necessary to use different classes according to different platforms, the Path class is good enough to used.
Next, let’s introduce how to use the Path class with a few examples!
Check if a file or folder exists
In the past, to check whether a file exists, a code similar to the following would be used for checking.
>>> import os
>>> os.path.exists('/tmp/myfile.txt')
If changed to the Path class, it would be in the following form:
>>> from pathlib import Path
>>> path = Path('/tmp/myfile.txt')
>>> path.exists()
False
From the above example, we can see that the exists()
method provided by the Path class can be used to determine whether a file exists. Overall, the code is more concise and understandable, and more object-oriented.
Create a file
People who have used Linux might know that you can create a file using the touch
command, and Path provides the same functionality.
>>> from pathlib import Path
>>> path = Path('/tmp', 'myfile.txt')
>>> path.exists()
False
>>> path.touch()
>>> path.exists()
True
As seen in the above example, the file is created after executing path.touch()
.
Obtain the filename / file extension
In the past, getting the filename and extension could be done through os.path.basename() and [os.path.splitext()](https
In the past, obtaining the file name and extension could be achieved through os.path.basename() and os.path.splitext(), respectively. With Path, the ’name’ and ‘suffix’ attributes are available, making it easy to obtain the file name and extension:
>>> from pathlib import Path
>>> path = Path('/tmp', 'myfile.txt')
>>> path.name
myfile.txt
>>>
>>> path.suffix
.txt
Write / Read files
The Path class also provides methods such as write_text() and read_text() to allow developers to easily write and read files:
>>> from pathlib import Path
>>> path = Path('/tmp', 'myfile.txt')
>>> path.write_text('Hello')
5
>>> path.write_text('World')
5
It should be noted that running the above example twice with write_text()
will not append the file content, but instead overwrite it, so read_text()
below will only read “World”:
>>> path.read_text()
'World'
In addition, the Path class also supports the with syntax and the open() method.
>>> with path.open('w') as f:
... f.write('Hello')
5
is_file / is_dir / is_symlink
The following code is used to judge whether the path is a file, folder, or symbol link in the past:
>>> import os
>>> os.path.isfile('/tmp/myfile.txt')
>>> os.path.isdir('/tmp/myfile.txt')
>>> os.path.islink('/tmp/myfile.txt')
These common methods are also integrated into the Path class:
>>> from pathlib import Path
>>> path = Path('/tmp', 'myfile.txt')
>>> path.touch()
>>> path.is_file()
True
>>> path.is_dir()
False
>>> path.is_symlink()
False
Of course, there are also more methods such as is_socket(), is_fifo(), is_block_device() that can be used.
Delete files
Also, the Path class provides a method to delete a file.
>>> from pathlib import Path
>>> path = Path('/tmp', 'myfile.txt')
>>> path.touch()
>>> path.is_file()
True
>>> path.unlink()
>>> path.exists()
False
After calling the unlink() method in the above example, the file is deleted.
Get the file size
The Path class also provides the stat() method to allow developers to obtain detailed information about the file, such as the file size.
We can create a very small file first with the following code:
>>> from pathlib import Path
>>> path = Path('/tmp', 'myfile.txt')
>>> path.touch()
>>> path.write_text('HelloWorld')
10
Then calling the stat()
method, you can see that the stat()
method returns os.stat_result where st_size
is the file size we need.
>>> path.stat()
os.stat_result(st_mode=33188, st_ino=20705682, st_dev=16777224, st_nlink=1, st_uid=501, st_gid=0, st_size=10, st_atime=1602572294, st_mtime=1602572354, st_ctime=1602572354)
Therefore, the file size 10 bytes can be obtained directly using path.stat().st_size
.
>>> path.stat().st_size
10
Traverse all files and folders in the folder
Another common task is to traverse all files and folders in a directory, which can be done by calling iterdir():
>>> from pathlib import Path
>>> for x in Path('/tmp').iterdir():
... print(type(x), x)
...
<class 'pathlib.PosixPath'> /tmp/com.google...
<class 'pathlib.PosixPath'> /tmp/com.apple...
<class 'pathlib.PosixPath'> /tmp/myfile.txt
<class 'pathlib.PosixPath'> /tmp/...
It is worth noting that each element yielded by iterdir()
is also an instance of the Path class, so you can also enjoy the various convenience functions provided by the Path class.
Path Operations
The Path class has an interesting feature that its instance can join paths by using the /
division operator, for example:
>>> Path('/home') / Path('abc')
PosixPath('/home/abc')
In addition, it is also possible to compare two Path class instances.
>>> Path('/tmp') == Path('/tmp')
True
>>> Path('/tmp/a') == Path('/tmp/b')
False
Conclusion
In addition to the functions and examples mentioned in this article, the Path class actually has quite a few useful functions that can be used. If you are interested, feel free to read the official documentation.
In addition, the official document also arranges a corresponding table between Path
and os
module related functions, which can be referred to Correspondence to tools in the os module.
References
https://docs.python.org/3/library/pathlib.html