I. What is Path?
This module provides classes that represent filesystem paths with semantics applicable to different operating systems. The path classes are categorized into pure paths, which provide pure computational operations without I/O, and specific paths, which are inherited from pure paths but provide I/O operations.
Pure paths are useful in some use cases, for example:
- If you want to manipulate Windows paths on a Unix device (or vice versa). You should not instantiate a WindowsPath on Unix, but you can instantiate PureWindowsPath.
- You only want to manipulate paths but not actually access the operating system. In this case, instantiating a pure path is useful because they don't have any operations that access the operating system.
II. Steps for use
1. Extract file name
Method name :.name
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") path_file_name = path_str.name print(path_file_name)
exports
2. Extract parent file path
Method name :.parent
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") path_parent_path = path_str.parent print(path_parent_path)
exports
\user\HinGwenWoong
3.Extract file suffix
Method name :.suffix
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") path_suffix = path_str.suffix print(path_suffix)
exports
.py
4. Extract file names without suffixes
Method name :.stem
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") path_only_name = path_str.stem print(path_only_name )
exports
demo
5. Change the file extension
Method name :.with_suffix
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") path_suffix = path_str.with_suffix(".json") print(path_suffix)
exports
\user\HinGwenWoong\
6. Traversing documents
Method name :.iterdir()
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/logs") for path in path_str.iterdir(): print(path)
exports
/user/HinGwenWoong/log/
/user/HinGwenWoong/log/
/user/HinGwenWoong/log/
/user/HinGwenWoong/log/
/user/HinGwenWoong/log/
7. Combined file path
Method name :.joinpath
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") path_str_join = path_str.joinpath("") print(path_str_join)
exports
\user\HinGwenWoong\
8. Whether the absolute path
Method name :is_absolute()
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") print(path_str.is_absolute())
exports
True
9. Whether folder or file
Method name :is_dir()
、 is_file()
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") print(path_str.is_dir()) print(path_str.is_file())
exports
True
False
10. Existence
Method name :.exists()
from pathlib import Path path_str = Path(r"/usr/HinGwenWoong/") print(path_str.exists())
exports
True
Method name :.glob
from pathlib import Path path_str = Path(r"/user/HinGwenWoong/scripts") print(path_str.glob('*.py'))
exports
[PosixPath('/user/HinGwenWoong/scripts/demo_1.py'),
PosixPath('/user/HinGwenWoong/scripts/demo_2.py')]
to this article on the file path extraction becomes more simple Python Path library is introduced to this article, more related Python Path library content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future !