#-*- 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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 作为新人,对于如何学好PYTHON也是一头雾雨,也很想能得到别人的帮助。今天看到这篇文章,感觉学习起来有了一个方向...
- 序言第1章 并行和分布式计算介绍第2章 异步编程第3章 Python的并行计算第4章 Celery分布式应用第5章...
- 注:本文的原文地址为 http://segmentfault.com/a/1190000000618286 许多 ...
- 序言第1章 并行和分布式计算介绍第2章 异步编程第3章 Python的并行计算第4章 Celery分布式应用第5章...
- 做python项目,需要用到mysql,一般用python-mysql,安装时遇到错误提示如下: Trace的关键...