#-*- coding: UTF-8 -*-
#__author__:Bing
#email:amazing_bing@outlook.com
#解壓和重新命名文件,并刪除zip文件或者目錄下的非PDF文件
import os,sys
import zipfile
def unzip_dir(file_name = "test",zipfilename="m:\\scan02.zip", unzipdirname="m:\\"):
fullzipfilename = os.path.abspath(zipfilename)
fullunzipdirname = os.path.abspath(unzipdirname)
#if not os.path.exists(fullzipfilename):
#Start extract files ...
try:
srcZip = zipfile.ZipFile(fullzipfilename, "r")
for eachfile in srcZip.namelist():
#print "Unzip file %s ..." % eachfile
eachfilename = os.path.normpath(os.path.join(fullunzipdirname, '{0}_{1}'.format(file_name,eachfile)))
eachdirname = os.path.dirname(eachfilename)
if eachfile.endswith(".pdf",4):
fd=open(eachfilename, "wb")
fd.write(srcZip.read(eachfile))
fd.close()
else:
pass
srcZip.close()
return {"status":1}
except:
return {"status":0}
unzip_dir()
def zip_dir(dirname, zipfilename):
filelist = []
#Check input ...
fulldirname = os.path.abspath(dirname)
fullzipfilename = os.path.abspath(zipfilename)
print "Start to zip %s to %s ..." % (fulldirname, fullzipfilename)
if not os.path.exists(fulldirname):
print "Dir/File %s is not exist, Press any key to quit..." % fulldirname
inputStr = raw_input()
return
if os.path.isdir(fullzipfilename):
tmpbasename = os.path.basename(dirname)
fullzipfilename = os.path.normpath(os.path.join(fullzipfilename, tmpbasename))
if os.path.exists(fullzipfilename):
print "%s has already exist, are you sure to modify it ? [Y/N]" % fullzipfilename
while 1:
inputStr = raw_input()
if inputStr == "N" or inputStr == "n" :
return
else:
if inputStr == "Y" or inputStr == "y" :
print "Continue to zip files..."
break
#Get file(s) to zip ...
if os.path.isfile(dirname):
filelist.append(dirname)
dirname = os.path.dirname(dirname)
else:
#get all file in directory
for root, dirlist, files in os.walk(dirname):
for filename in files:
filelist.append(os.path.join(root,filename))
#Start to zip file ...
destZip = zipfile.ZipFile(fullzipfilename, "w")
for eachfile in filelist:
destfile = eachfile[len(dirname):]
print "Zip file %s..." % destfile
destZip.write(eachfile, destfile)
destZip.close()
print "Zip folder succeed!"
# def download(domain):
# import urllib2
# print "downloading with urllib2"
# url = 'http://scan02.secfuture.com:8081/wvs_scan_getresult/?id=2'
# f = urllib2.urlopen(url)
# data = f.read()
# with open("m:\\{0}.zip".format(domain), "wb") as code:
# code.write(data)
# code.close()
python-unzip
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 作為新人,對(duì)于如何學(xué)好PYTHON也是一頭霧雨,也很想能得到別人的幫助。今天看到這篇文章,感覺學(xué)習(xí)起來有了一個(gè)方向...
- 序言第1章 并行和分布式計(jì)算介紹第2章 異步編程第3章 Python的并行計(jì)算第4章 Celery分布式應(yīng)用第5章...
- 注:本文的原文地址為 http://segmentfault.com/a/1190000000618286 許多 ...
- 序言第1章 并行和分布式計(jì)算介紹第2章 異步編程第3章 Python的并行計(jì)算第4章 Celery分布式應(yīng)用第5章...
- 做python項(xiàng)目,需要用到mysql,一般用python-mysql,安裝時(shí)遇到錯(cuò)誤提示如下: Trace的關(guān)鍵...