1. os模块相关
The os
module is a standard library module in Python that provides functions for interacting with the operating system. It allows you to perform various operations related to file and directory manipulation, process management, and environment variables.
译文:os
模块是Python中的一个标准库模块,它提供了与操作系统进行交互的函数。它允许您执行与文件和目录操作、进程管理和环境变量相关的各种操作。
按照使用频率:
os.getcwd()
: Returns the current working directory as a string.os.listdir(path)
: Returns a list containing the names of all files and directories in the specified path.os.makedirs(path)
: Used to recursively create directories. It takes a path as a parameter and creates all the directories in the path if they do not already exist.os.path.join(path1, path2)
: Joins two or more path name components to form a complete path.**os.path.exists(path)
: Returns True if the path exists, otherwise returns False.
个人认为以下函数低频:os.chdir(path)
: Changes the current working directory to the specified path.os.mkdir(path)
: Creates a new directory with the specified path.os.rmdir(path)
: Removes the directory with the specified path.os.path.isfile(path)
: Returns True if the path points to a file, otherwise returns False.os.path.isdir(path)
: Returns True if the path points to a directory, otherwise returns False.os.path.basename(path)
: Returns the base name of the file or directory(文件or文件夹) in the specified path.
These are just a few examples of the functions provided by the os
module. There are many more functions available for various operating system-related operations. You can find more information about the os
module in the Python documentation.
2. Path模块相关
The path
module in Python provides functions for working with file paths. It is part of the standard library and does not require any additional installation.
Here is an example of how to use the path
module:
import os
# Get the current working directory
current_directory = os.getcwd()
print("Current directory:", current_directory)
# Join two path components
path1 = 'C:\\Users'
path2 = 'User1'
full_path = os.path.join(path1, path2)
print("Full path:", full_path)
# Check if a path exists
exists = os.path.exists(full_path)
print("Path exists:", exists)
# Check if a path is a file
is_file = os.path.isfile(full_path)
print("Path is a file:", is_file)
# Check if a path is a directory
is_dir = os.path.isdir(full_path)
print("Path is a directory:", is_dir)
# Split a path into its components
dir_name, file_name = os.path.split(full_path)
print("Directory name:", dir_name)
print("File name:", file_name)
# Get the file extension
extension = os.path.splitext(full_path)[1]
print("File extension:", extension)
This code snippet demonstrates some of the common operations you can perform using the path
module, such as getting the current working directory, joining path components, checking if a path exists, checking if it is a file or directory, splitting a path into its directory and file components, and getting the file extension.
Note that in the example code above, we import the os
module instead of the path
module. This is because the path
module is part of the os
module namespace.
3. shutil 模块
The shutil
module is a Python module that helps in performing high-level file operations. It provides functions to copy, move, and remove files and directories.
译文:shutil模块是一个Python模块,用于执行高级文件操作。它提供了复制、移动和删除文件和目录的功能
Here is an example of using the shutil
module to perform file operations:
import shutil
# Copy a file
shutil.copy('source_file.txt', 'destination_file.txt')
# Copy a directory
shutil.copytree('source_directory', 'destination_directory')
# Move a file
shutil.move('source_file.txt', 'destination_file.txt')
# Remove a file
shutil.remove('file.txt')
# Remove a directory
shutil.rmtree('directory')
Note that the shutil
module also provides other functions that can be used for various file operations such as archiving, file compression, etc.
4. tqdm Python库 :显示进度条用
tqdm is a Python library that provides a progress bar for loops and other iterative processes.
备注:It stands for “tqdm” which means “taqaddum” in Arabic, which translates to “progress” in English. It is often used to track the progress of tasks that may take a long time to complete, such as downloading files, training machine learning models, or processing large amounts of data.
With tqdm, you can easily wrap your loops or iterable objects with a progress bar that gives you real-time updates on how much of the task has been completed. It provides an intuitive and colorful progress bar, along with an ETA (estimated time of arrival) that gives you an idea of how much longer the task will take to complete.
To use tqdm, you first need to install it using pip:
pip install tqdm
Once installed, you can import it in your Python program and wrap your loop with the tqdm function. Here’s an example:
from tqdm import tqdm
持续更新中…敬请期待!