16.5. Pathlib Name
Path.name- Get the name of the file or directoryPath.stem- Get the name of the file without the suffixPath.suffix- Get the file extensionPath.suffixes- Get all file extensions (for files with multiple extensions)Path.parent- Get the parent directory of the pathPath.parts- Get all parts of the path as a tuplePath.with_name(new_name)- Change the name of the file or directoryPath.with_suffix(new_suffix)- Change the file extension
16.5.1. SetUp
>>> from pathlib import Path
>>>
>>> myfile = Path('/home/myuser/myfile.txt')
16.5.2. Name
>>> myfile.name
'myfile.txt'
16.5.3. Stem
>>> myfile.stem
'myfile'
16.5.4. Suffix
>>> myfile.suffix
'.txt'
16.5.5. Suffixes
>>> myfile.suffixes
['.txt']
16.5.6. Parent
>>> myfile.parent
PosixPath('/home/myuser')
16.5.7. Parts
>>> myfile.parts
('/', 'home', 'myuser', 'myfile.txt')
16.5.8. With Name
>>> newfile = myfile.with_name('newfile.md')
>>> newfile
PosixPath('/home/myuser/newfile.md')
16.5.9. With Suffix
>>> newfile = myfile.with_suffix('.md')
>>> newfile
PosixPath('/home/myuser/myfile.md')