Unzip the zip file and delete the instance of the file under Python _python

Source: Internet
Author: User
Tags extract zip file
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!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.