For Python 3.4 or later, you should just throw away os.path and use pathlib

Pathlib has been added as a module to perform file path operations in Python 3.4.

Until now, file path operations used os.path and glob, but using pathlib makes continuous file path operations very easy to write, so you should actively use them.

Features of pathlib

pathlib has the following features.

Object-orientation

In os.path etc. that I used until now, the function was provided by an independent function.

On the other hand, pathlib provides functions as methods and properties of the Path class.

pathlib


>>> from pathlib import Path
>>> Path('/a/b/c.txt').parent
PosixPath('/a/b')
>>> Path('/a/b/c.txt').parents[1]
PosixPath('/a')
>>> Path('/a/b').joinpath('c/d')
PosixPath('/a/b/c/d')
>>> Path('/a/b') / 'c/d'
PosixPath('/a/b/c/d')

The return value is also a Path object instead of str.

Integration of file path manipulation functions

Until now, file path manipulation functions were distributed not only to os.path but also to multiple modules such as os, glob and the built-in function open, but most of them are in pathlib. Features are integrated.

pathlib glob


Path('.').glob('*')

pathlib open


Path('a.txt').open('w')

Also, since Python 3.6, some of the modules used so far have been changed so that they can receive not only str but also Path objects as arguments.

Built-in function open()Pass a Path object to


open(Path('a.txt'), 'w')

Benefits of using pathlib

With the explanation so far, some people may think, "Isn't it just more work to create an instance?"

However, ** pathlib is most useful when performing continuous path operations. ** **

As an example, let's compare the process of checking if a file called hoge.txt exists in the parent directory of the directory where a file is stored.

First, when using os.path.

os.path


>>> os.path.exists(os.path.join(os.path.dirname(os.path.dirname('/a/b/c.txt')), 'hoge.txt'))
False

Function calls are nested and at first glance you can't immediately understand what you're doing.

Next is the case of using pathlib.

pathlib part 1


>>> Path('/a/b/c.txt').parent.parent.joinpath('hoge.txt').exists()
False

pathlib part 2


>>> (Path('/a/b/c.txt').parents[1] / 'hoge.txt').exists()
False

It can be read naturally from left to right, which makes it very easy to understand.

As you can see, ** pathlib makes continuous path operations very easy to read (write). ** **

Is pathlib widespread?

I haven't used pathlib in practice yet, and I haven't seen the code used.

I tried to find out how much it is actually used.

Google Trends

https://trends.google.co.jp/trends/explore?date=today%205-y&q=python%20os.path,python%20pathlib

python_os_path__python_pathlib_-_調べる_-_Google_トレンド.png

The number of searches is increasing, but not as much as os.path yet.

GitHub

I couldn't search the date with Code, so check it with Issue.

Year os.path pathlib
2014 293 77
2015 897 181
2016 826 342
2017 463 448

Pathlib is steadily increasing, and it seems that os.path will be overtaken this year.

Qiita

Check it with [Qii Trend].

https://qiitrend.herokuapp.com/trend?mode=count&period=4&query=tag%3Apython+os.path&query=tag%3Apython+pathlib&unit=yearly

QiiTrend.png

In Qiita's article, pathlib is almost unused.

Judging from the results of GitHub and Qiita, does it mean that the spread is delayed only in Japan?

Recommended Posts

For Python 3.4 or later, you should just throw away os.path and use pathlib
Throw away curl and use httpie
[Python] Use and and or when creating variables
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
You should know if you use Python! 10 useful libraries
Use data class for data storage of Python 3.7 or higher
% And str.format () in Python. Which one do you use?
Update Python for Raspberry Pi to 3.7 or later with pyenv
Have Python act for you, then your eyes and brain ...
Which should I study, R or Python, for data analysis?