pathlib Are you all using it?

Introduction

** pathlib ** Do you use it? If you haven't used it yet, please use it once. What is pathlib is that you can manipulate paths. I think everyone uses ** os.path **, but you can do almost the same thing as that os.path with ** pathlib **. Moreover, pathlib is a standard Python module.

Who is the target of this article

--Those who have never used pathlib --People who know pathlib but haven't made it yet --Those who are interested in pathlib

If you are interested, please check it out.

summary

--Get file name only ** filename.name ** --Get filename without extension ** filename.stem ** --File extension only ** filename.suffix ** --Read file ** list (dir_path.iterdir ()) **, ** list (dir_path.glob ('*')) ** --Create directory (also intermediate directory) ** dir_path.mkdir (parents = True) ** --Create directory (no error even if it exists) ** dir_path.mkdir (exist_ok = True) ** --Create new file ** filename.touch () **

pathlib

pathlib is a ** object-oriented file system path **. Let's see how to use it.

constructor

You can create a Path object withPath ()

>>> from pathlib import Path
>>> Path()
PosixPath('.')
>>> Path('data')
PosixPath('data')
>>> 
>>> Path('data/image_0001.jpg')
PosixPath('data/image_0001.jpg')
>>> 

Determining whether it is a directory or a file

Use ʻis_dir (), ʻis_file ()

>>> a = Path('data')
>>> a.is_dir()
True
>>> b = Path('data/image_0001.jpg')
>>> b.is_file()
True
>>> 

Check for non-existent files

>>> p = Path('data/img0.jpg')
>>> p.exists()
False

Create empty file

Use .touch ()

>>> p = Path('data/img0.jpg')
>>> p.exists()
False
>>> p.touch()
>>> p.exists()
True

Get a list of files directly under the directory

Use ʻiterdir () or glob () `

>>> a = Path('data')
>>> list(a.iterdir())
[PosixPath('data/img0.jpg'), 
PosixPath('data/image_0004.jpg'),
PosixPath('data/image_0005.jpg'), 
PosixPath('data/image_0001.jpg'), 
PosixPath('data/image_0002.jpg'), 
PosixPath('data/image_0003.jpg')]
>>> 


>>> list(a.glob('*'))
[PosixPath('data/img0.jpg'), 
PosixPath('data/image_0004.jpg'), 
PosixPath('data/image_0005.jpg'), 
PosixPath('data/image_0001.jpg'), 
PosixPath('data/image_0002.jpg'), 
PosixPath('data/image_0003.jpg')]
>>> 

Convert all file names to strings

Convert to a string with str

>>> [str(p) for p in a.iterdir()]
['data/img0.jpg', 
'data/image_0004.jpg', 
'data/image_0005.jpg', 
'data/image_0001.jpg', 
'data/image_0002.jpg', 
'data/image_0003.jpg']
>>> 

Get only the file name (at the end of the path)

Use the name attribute

>>> [p.name for p in a.iterdir() if p.is_file()]
['img0.jpg', 
'image_0004.jpg', 
'image_0005.jpg', 
'image_0001.jpg', 
'image_0002.jpg', 
'image_0003.jpg']
>>> 

Get file name without extension

Use the stem attribute

>>> [p.stem for p in a.iterdir() if p.is_file()]
['img0', 
'image_0004', 
'image_0005', 
'image_0001', 
'image_0002', 
'image_0003']
>>> 

Get the file name extension

Use the shuffix attribute

>>> [p.suffix for p in a.iterdir() if p.is_file()]
['.jpg', '.jpg', '.jpg', '.jpg', '.jpg', '.jpg']
>>> 

Creating a directory

Use .mkdir ()

>>> c = Path('test')
>>> c.exists()
False
>>> c.mkdir()
>>> c.exists()
True
>>> 

Create intermediate directories together

Set to parents = True

>>> Path('test/data/001').mkdir(parents=True)

Don't get an error even if the directory exists

Set ʻexist_ok = True`

>>> Path('test/data').mkdir(exist_ok=True)

How to connect paths

Concatenate with / to the Path object You can do the same with / as ʻos.path.join ()`

>>> d = Path('data')
>>> d / 'train' / '001'
PosixPath('data/train/001')

Get parent directory

Use parent

>>> b
PosixPath('data/image_0001.jpg')
>>> b.parent
PosixPath('data')

At the end

This time I wrote it as my memorandum so that I don't forget the ones I use often. I hope it will be helpful as much as possible.

References

--Object-oriented file system path

-Get filename / extension / parent directory with Python, pathlib

Recommended Posts

pathlib Are you all using it?
About the development environment you are using
Check the type of the variable you are using
What are you using when testing with Python?
Are you wearing a mask? Program development (using Yolov5)
Where are you from
(Python3) No. oO (Are you using the standard library?): 5 shaders
Checking if you are Sudoku