利用python下載數據,下載下來的數據爲zip格式,因爲有上千個這樣的文件,因此便直接在爬蟲程序里加入瞭解壓縮zip文件的內容,並且因爲數據量較大,爲了節省空間,便在解壓縮後立即刪除該zip文件。
先來介紹解壓縮的方法:
import zipfile
filename = '/home/username/work/1.zip'
fz = zipfile.ZipFile(filename, 'r')
for file in fz.namelist():
fz.extract(file, path)
下面是刪除文件:
若文件存在,則刪除。
import os
if os.path.exists(filename):
os.remove(filename)
下面是刪除文件夾的方法:
import os
# 刪除空文件夾,若文件夾非空,會報錯
path = '/home/username/work/one/'
os.rmdir(path)
# 刪除非空文件夾或空文件夾,更強大
import shutil
shutil.rmtree(path)
# 判斷文件夾是否存在,方法和判斷文件存在與否相同
if os.path.exists(path):
os.rmdir(path)
以上,歡迎交流!
主要參考:http://jingyan.baidu.com/article/39810a23e80384b636fda639.html