** 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.
--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.
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')
>>>
Use ʻis_dir (), ʻis_file ()
>>> a = Path('data')
>>> a.is_dir()
True
>>> b = Path('data/image_0001.jpg')
>>> b.is_file()
True
>>>
>>> p = Path('data/img0.jpg')
>>> p.exists()
False
Use .touch ()
>>> p = Path('data/img0.jpg')
>>> p.exists()
False
>>> p.touch()
>>> p.exists()
True
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 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']
>>>
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']
>>>
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']
>>>
Use the shuffix
attribute
>>> [p.suffix for p in a.iterdir() if p.is_file()]
['.jpg', '.jpg', '.jpg', '.jpg', '.jpg', '.jpg']
>>>
Use .mkdir ()
>>> c = Path('test')
>>> c.exists()
False
>>> c.mkdir()
>>> c.exists()
True
>>>
Set to parents = True
>>> Path('test/data/001').mkdir(parents=True)
Set ʻexist_ok = True`
>>> Path('test/data').mkdir(exist_ok=True)
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')
Use parent
>>> b
PosixPath('data/image_0001.jpg')
>>> b.parent
PosixPath('data')
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.
--Object-oriented file system path
-Get filename / extension / parent directory with Python, pathlib
Recommended Posts