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')