Python 3 Pathlib tutorial: high -efficiency operating files and directory
Python 3 Pathlib tutorial: efficient operation files and directory
In Python 3, the `Pathlib` module provides an efficient and intuitive way to operate files and directory.It is an object -oriented method that allows us to manipulate the file path in a more concise and readability.
This tutorial will guide you to understand how to use the `Pathlib` module of Python 3 to perform files and directory operations.
1. Import `Pathlib` module
First, we need to import the `Pathlib` module.Use the following code to import `Pathlib`:
python
from pathlib import Path
2. Create path object
To create a path object, we can use the constructor of the `Path` class.The constructor accepts a string parameter to indicate the path of the file or directory.The following are examples of creating path objects:
python
file_path = Path('path/to/file.txt')
directory_path = Path('path/to/directory')
Make sure the path string is replaced with the actual file path or directory path.
3. File and directory operation
`Pathlib` provides many convenient methods to perform operations related to file and directory.
-Keng whether the path exists
To check whether the path exists, we can use the `Exist ()" method.The following is an example of using this method:
python
if file_path.exists():
Print ("file existence")
-Colon the file name and directory name
To obtain file names (including extension names) and directory names, we can use the `name` attributes and` paint` attributes.Here are examples of obtaining file names and directory names:
python
file_name = file_path.name
directory_name = directory_path.name
Print ("file name:", file_name)
Print ("Directory Name:", Directory_name)
- Create a directory
To create a new directory, we can use the `mkdir () method.The following is an example of creating a directory:
python
new_directory_path = Path("new_directory")
new_directory_path.mkdir()
-The traversed directory
To traverse the files and sub -directory in the directory, we can use the method of `iterdir ().Here are examples of traversal directory:
python
for item in directory_path.iterdir():
if item.is_file():
Print ("File:", item.name)
elif item.is_dir():
Print ("Directory:", item.name)
-Wef files and directory
To delete files or directory, we can use the method of `unlink ()` and `rmdir ().The following are examples of deleting files and directory:
python
file_path.unlink()
directory_path.rmdir()
Please note that if the directory is not empty, the method of using the `rmdir ()` method will trigger the `oserror`.You can use the `PATH.RGLOB () method to recover all the files and sub -directory in the directory.
This is some basic usage of `Pathlib`.With these methods and other methods provided by other `Pathlib`, you can operate files and directory in a very simple and efficient way.
This is a complete example code that demonstrates how to use `Pathlib`:
python
from pathlib import Path
file_path = Path('path/to/file.txt')
directory_path = Path('path/to/directory')
if file_path.exists():
Print ("file existence")
file_name = file_path.name
directory_name = directory_path.name
Print ("file name:", file_name)
Print ("Directory Name:", Directory_name)
new_directory_path = Path("new_directory")
new_directory_path.mkdir()
for item in directory_path.iterdir():
if item.is_file():
Print ("File:", item.name)
elif item.is_dir():
Print ("Directory:", item.name)
file_path.unlink()
directory_path.rmdir()
Please make corresponding modifications and configurations according to your needs.
Using the `Pathlib` module can greatly simplify the code of files and directory operations.It provides a more intuitive and efficient way to process files and directory paths.Compared with traditional string operations, the method provided by `Pathlib` is more readable and easy to maintain.With `Pathlib`, we can easily complete the manipulation of files and directory.