Outline of this article
The os module is an important module in the Python standard library, which provides common operations on directories and files. And another Python standard library-shutil library, as a supplement to the os module, provides operations such as copy, move, delete, compress, and decompress. These os modules are generally not provided. But it should be noted that: The shutil module processes the compressed package by calling two modules, ZipFile and TarFile.
Knowledge talk
The materials used in this article are based on the following two folders, one of which is empty.
1) Module import
import shutil
2) Copy files
- Function: shutil.copy(src,dst)
- Meaning: copy files;
- Parameters: src represents the source file, dst represents the target folder;
- Note: When moving to a non-existent "target folder", the system will recognize the non-existent "target folder" as a new folder without reporting an error;
# 1.将a表的“data.txt”移动到b表
src = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a\data.txt"
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_b"
shutil.copy(src,dst)
------------------------------------------------------------
# 2.将a表的“data.txt”移动到b表,并重新命名为“new_data.txt”
src = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a\data.txt"
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_b\new_data.txt"
shutil.copy(src,dst)
------------------------------------------------------------
# 3.将a表的“data.txt”移动到“不存在”的文件夹
src = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a\data.txt"
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_c"
shutil.copy(src,dst)
"""
注意:对于情况3,系统会默认将“test_shutil_c”识别为文件名,而不是按照我们认为的,移动到一个新的不存在的文件夹。
"""
The results are as follows:
3) Copy folder
- Function: shutil.copytree(src,dst)
- Meaning: copy the folder;
- Parameters: src represents the source folder, dst represents the target folder;
- Note: This can only be moved to an empty folder, not a non-empty folder containing other files, otherwise PermissionError will be reported;
① If there are other files in the target folder, an error will be reported;
# 将a文件夹移动到b文件夹,由于前面的操作,此时b文件夹中已经有其他文件
src = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a"
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_b"
shutil.copytree(src,dst)
The results are as follows:
② If you specify any target folder, it will be created automatically;
# c文件夹原本是不存在的,我们使用了下方的代码,会自动创建该文件夹
src = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a"
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_c"
shutil.copytree(src,dst)
The results are as follows:
4) Move files or folders
- Function: shutil.move(src,dst)
- Meaning: move files/folders;
– Parameters: src means source files/folders, dst means target folders; - Note: Once the file/folder is moved, the original file/folder is gone. When the target folder does not exist, an error will be reported;
# 将当前工作目录下的“a.xlsx”文件,移动到a文件夹下
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a"
shutil.move("a.xlsx",dst)
----------------------------------------------------------------
# 将a文件夹下的“a.xlsx”文件,移动到b文件夹中,并重新命名为“aa.xlsx”
src = r"C:/Users/黄伟/Desktop/publish/os模块/test_shutil_a\a.xlsx"
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_b\aa.xlsx"
shutil.move(src,dst)
The results are as follows:
Note: The operation of moving folders is similar, I won't repeat them here, and learn by myself.
5) Delete the folder (use with caution)
- Function: shutil.rmtree(src)
- Meaning: delete the folder;
- Parameters: src represents the source folder;
- Note: The difference here is the usage of remove() and rmdir() in the os module. The remove() method can only delete a file, and mdir() can only delete an empty folder. But the rmtree() in the shutil module can completely delete non-empty folders recursively;
# 将c文件夹彻底删除
src = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_c"
shutil.rmtree(src)
The results are as follows:
6) Create and decompress the compressed package
- zipobj.write(): Create a compressed package;
- zipobj.namelist(): read the file information in the compressed package;
- zipobj.extract(): decompress a single file in the compressed package;
- zipobj.extractall(): decompress all files in the compressed package;
- The shutil module processes the compressed package by calling the two modules ZipFile and TarFile, so these two modules need to be imported;
- Note: The compressed package mentioned here refers to the compressed package in ".zip" format;
① Create a compressed package
import zipfile
import os
file_list = os.listdir(os.getcwd())
# 将上述所有文件,进行打包,使用“w”
with zipfile.ZipFile(r"我创建的压缩包.zip", "w") as zipobj:
for file in file_list:
zipobj.write(file)
The results are as follows:
② Read the file information in the compressed package
import zipfile
with zipfile.ZipFile("我创建的压缩包.zip", "r") as zipobj:
print(zipobj.namelist())
The results are as follows:
③ Unzip a single file in the compressed package
- Note: The target folder does not exist, it will be created automatically;
import zipfile
# 将压缩包中的“test.ipynb”文件,单独解压到a文件夹下
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_a"
with zipfile.ZipFile("我创建的压缩包.zip", "r") as zipobj:
zipobj.extract("test.ipynb",dst)
The results are as follows:
④ Unzip all the files in the compressed package;
- Note: The target folder does not exist, it will be created automatically;
import zipfile
# 将压缩包中的所有文件,解压到d文件夹下
dst = r"C:\Users\黄伟\Desktop\publish\os模块\test_shutil_d"
with zipfile.ZipFile("我创建的压缩包.zip", "r") as zipobj:
zipobj.extractall(dst)
The results are as follows: