Below for you to share a Python extract zip file and delete the instance of the file, with a good reference value, I hope to help you. Come and see it together.
Using Python to download data, the downloaded data is in the ZIP format, because there are thousands of such files, so directly in the crawler to learn the contents of the compressed zip file, and because of the large amount of data, in order to save space, the zip file will be deleted immediately after decompression.
First, we introduce the method of decompression:
Import zipfilefilename = '/home/username/work/1.zip ' FZ = ZipFile. ZipFile (filename, ' r ') for file in Fz.namelist (): fz.extract (file, path)
Here is the delete file:
If the file exists, delete it.
Import osif os.path.exists (filename): os.remove (filename)
Here's how to delete a folder:
Import os# Delete empty folder, if the folder is not empty, will error path = '/home/username/work/one/' os.rmdir (path) #删除非空文件夹或空文件夹, more powerful import Shutilshutil.rmtree (Path) #判断文件夹是否存在, the method and determine whether the file exists or not is the same if os.path.exists (path): os.rmdir (PATH)
Above, Welcome to exchange!