美文网首页
python-unzip

python-unzip

作者: amazing_bing | 来源:发表于2017-03-31 11:30 被阅读0次
    #-*- 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

          本文链接:https://www.haomeiwen.com/subject/qtamottx.html