In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use the shutil function of the python module. I hope you will get something after reading this article. Let's discuss it together.
Outline of this article
The os module is an important module in the Python standard library, which provides common operations on directories and files. Shutil library, another standard library of Python, as a supplement to os module, provides copy, move, delete, compression, decompression and other operations, which are generally not provided in these os modules. But it should be noted that the shutil module handles the compressed package by calling the ZipFile and TarFile modules.
Knowledge string talk
The material used in this article is based on the following two folders, one of which is empty.
1) Module Import import shutil2) copy file
Function: shutil.copy (src,dst) meaning: copy files; parameters: src represents source files, dst represents destination folder; Note: when you move to a non-existent "destination folder", the system will recognize the non-existent "destination folder" as a new folder without reporting an error.
# 1. Move the "data.txt" of table a to table b src = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a\ data.txt" dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_b" shutil.copy (src Dst)-# 2. Move the "data.txt" of table a to table b And rename it to "new_data.txt" src = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a\ data.txt" dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_b\ new_data.txt" shutil.copy (src Dst)-# 3. Move the "data.txt" of table a to the folder src = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a\ data.txt" dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_c" shutil.copy (src,dst) "Note: for case 3, the system will recognize" test_shutil_c "as the file name by default, not as we think Move to a new folder that does not exist. "
The results are as follows:
3) copy folder
Function: shutil.copytree (src,dst)
Meaning: copy folder
Parameter: src represents the source folder, dst represents the destination folder
Note: you can only move to an empty folder, not a non-empty folder containing other files, otherwise an error PermissionError will be reported.
① will report an error if there are other files in the destination folder
# move a folder to b folder. As a result of the previous operation, there are other files src = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a" dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_b" shutil.copytree (src,dst)
The results are as follows:
If ② specifies any of the destination folders, it will be created automatically
The # c folder originally does not exist. We use the following code to automatically create the folder src = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a" dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ 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
Parameter: src represents the source file / folder, dst represents the destination folder
Note: once the file / folder is moved, the file / folder in the original location is gone. An error will be reported when the destination folder does not exist
# change the "a.xlsx" file in the current working directory Move to a folder dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a" shutil.move ("a.xlsx", dst)-# change the "a.xlsx" file under a folder Move to the b folder and rename it to "aa.xlsx" src = r "C:/Users/ Huang Wei / Desktop/publish/os module / test_shutil_a\ a.xlsx" dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_b\ aa.xlsx" shutil.move (src,dst)
The results are as follows:
Note: the operation of moving folders is similar, so I won't repeat it here. Go down and learn by yourself.
5) Delete folders (use with caution)
Function: shutil.rmtree (src)
Meaning: delete folder
Parameter: src represents the source folder
Note: unlike the use 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 rmtree () in the shutil module can recursively delete non-empty folders completely.
# completely delete the c folder src = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_c" shutil.rmtree (src)
The results are as follows:
6) create and decompress the 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 the files in the package
The shutil module handles the compressed package by calling ZipFile and TarFile, so these two modules need to be imported.
Note: the compressed package here refers to the compressed package in ".zip" format.
① creates a compressed package
Import zipfileimport osfile_list = os.listdir (os.getcwd ()) # package all the above files, using "w" with zipfile.ZipFile (r "zip package I created", "w") as zipobj: for file in file_list: zipobj.write (file)
The results are as follows:
② reads the file information in the compressed package
Import zipfilewith zipfile.ZipFile ("zip package I created", "r") as zipobj: print (zipobj.namelist ())
The results are as follows:
③ unzips a single file in the package. Note: the destination folder does not exist and will be created automatically.
Import zipfile# decompresses the "test.ipynb" file in the archive to a folder dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_a" with zipfile.ZipFile ("the zip I created", "r") as zipobj: zipobj.extract ("test.ipynb", dst)
The results are as follows:
④ unzips all files in the package. Note: the destination folder does not exist and will be created automatically.
Import zipfile# unzips all the files in the compressed package to the d folder dst = r "C:\ Users\ Huang Wei\ Desktop\ publish\ os module\ test_shutil_d" with zipfile.ZipFile ("the zip package I created", "r") as zipobj: zipobj.extractall (dst)
The results are as follows:
After reading this article, I believe you have a certain understanding of "how to use the shutil function of python module". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.