加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python写的将一些类型的文件压缩为7z格式的脚本

(2010-04-18 07:54:18)
标签:

杂谈

#将一些类型的文件压缩为7z.py
#for folder all file do 7z
import os
import sys
import distutils.file_util

def ImportOkFile():
    if(os.path.isfile("D:\\Records\\将一些类型的文件压缩为7z_record.txt")==False):
        f=open("D:\\Records\\将一些类型的文件压缩为7z_record.txt","w")
        f.close()
    f=open("D:\\Records\\将一些类型的文件压缩为7z_record.txt","r")
    okList=f.readlines()
    f.close()
    doneOkList=[]
    for okFile in okList:
        if(okFile[:3]=="ok "):
            doneOkList.append(okFile[3:-1])
    return doneOkList

def RecordOkFile(file,preinfo="ok "):
    f=open("D:\\Records\\将一些类型的文件压缩为7z_record.txt","a")
    f.write(preinfo+file+"\n")
    f.close()

def OneLayDir(doDir):
    folderfile=os.listdir(doDir)
    if len(folderfile)==1 and os.path.isdir(doDir+"\\"+folderfile[0]):
        firstfolder=curfolder=doDir+"\\"+folderfile[0]+"\\"
        folderfile=os.listdir(curfolder)
        while len(folderfile)==1 and os.path.isdir(curfolder+folderfile[0]):
            folderfile=os.listdir(curfolder)
            curfolder=curfolder+folderfile[0]+"\\"
        try:
            folderfile=os.listdir(curfolder)
        except execiption as e:
            return False;
        pathExistPath=[]
        for file in folderfile:
            if os.path.isdir(curfolder+file):
                if os.path.exists(doDir+"\\"+file):
                    os.system("move /y \""+curfolder+file+"\" \""+doDir+"\\"+file+"2\"")
                    pathExistPath.append(doDir+"\\"+file+"2")
                else:
                    os.system("move /y \""+curfolder+file+"\" \""+doDir+"\\"+file+"\"")
            else:
                os.system("move /Y \""+curfolder+file+"\" \""+doDir+"\"")
        os.system("rmdir /S /Q \""+firstfolder+"\"")
        for folder in pathExistPath:
            os.system("move /y \""+folder+"\" \""+folder[:-1]+"\"")
        return True

def dodir(fld,okFiles):
    OneLayDir(fld)
    files=os.listdir(fld)
    for f in files:
        file=os.path.join(fld,f)
        if file not in okFiles and file.find("")==0:
            if os.path.isdir(file):
                dodir(file,okFiles)
            elif os.path.isfile(file):
                if file[-3:].lower()==".7z":
                    print("extract:"+file)
                    os.system("D:\\7z.exe x -y -o\""+file[:-3].strip()+"\" \""+file+"\"")
                    if(os.path.exists(file[:-3].strip()) and len(os.listdir(file[:-3].strip()))!=0):
                        os.system("del /F \""+file+"\"")
                        print("deleted old file:"+file)
                    else:
                        RecordOkFile(file,"extract fail:")
                        continue
                    onedir=os.listdir(file[:-3].strip())
                    extdir="*"
                    if(OneLayDir(file[:-3].strip())==False):
                        RecordOkFile(file,"one lay dir fail:")
                        continue
                    print("D:\\7z.exe a -y -mx9 \""+file[:-3].strip()+".7z\" \""+file[:-3].strip()+"\\"+extdir+"\"")
                    os.system("D:\\7z.exe a -y -mx9 \""+file[:-3].strip()+".7z\" \""+file[:-3].strip()+"\\"+extdir+"\"")
                    os.system("rmdir /S /Q \""+file[:-3].strip()+"\"")
                    RecordOkFile(file)
                elif file[-4:].lower()==".rar" or file[-4:].lower()==".zip":
                    if file.lower().find(".part")==-1:
                        print("extract:"+file)
                        os.system("D:\\7z.exe x -y -o\""+file[:-4].strip()+"\" \""+file+"\"")
                        onedir=os.listdir(file[:-4].strip())
                        extdir="*"
                        if(OneLayDir(file[:-3].strip())==False):
                            RecordOkFile(file,"one lay dir fail:")
                            continue
                        print("D:\\7z.exe a -y -mx9 \""+file[:-4].strip()+".7z\" \""+file[:-4].strip()+"\\"+extdir+"\"")
                        os.system("D:\\7z.exe a -y -mx9 \""+file[:-4].strip()+".7z\" \""+file[:-4].strip()+"\\"+extdir+"\"")
                        os.system("rmdir /S /Q \""+file[:-4].strip()+"\"")
                    else:
                        RecordOkFile(file,"noDo:")
                        continue
                    rarfile=open(file,"r")
                    rarfilelen=rarfile.seek(0,2)
                    rarfile.close()
                    szfile=open(file[:-4].strip()+".7z","r")
                    szfilelen=szfile.seek(0,2)
                    szfile.close()
                    if(szfilelen<rarfilelen):
                        os.system("del /F \""+file+"\"")
                        RecordOkFile(file,"select delete "+file[-4:]+":")
                    else:
                        os.system("del /F \""+file[:-4].strip()+".7z"+"\"")
                        RecordOkFile(file[:-4].strip()+".7z","select delete 7z:")
                    RecordOkFile(file)
                elif  file[-4:].lower()==".pdf" or file[-4:].lower()==".exe" or  file[-4:].lower()==".chm":
                    print("D:\\7z.exe a -y -mx9 \""+file[:-4].strip()+".7z\" \""+file+"\"")
                    os.system("D:\\7z.exe a -y -mx9 \""+file[:-4].strip()+".7z\" \""+file+"\"")
                    pEcFile=open(file,"r")
                    pEcFileLen=pEcFile.seek(0,2)
                    pEcFile.close()
                    szFile=open(file[:-4].strip()+".7z","r")
                    szFileLen=szFile.seek(0,2)
                    szFile.close()
                    if(szLileLen<pEcFileLen):
                        os.system("del /F \""+file+"\"")
                        RecordOkFile(file,"select delete "+file[-4:]+":")
                    else:
                        os.system("del /F \""+file[:-4].strip()+".7z"+"\"")
                        RecordOkFile(file[:-4].strip()+".7z","select delete 7z:")
                    RecordOkFile(file)
                else:
                    RecordOkFile(file,"noDO:")
                    continue
               

okFiles=ImportOkFile()
print("Import ok files:"+str(len(okFiles)))
dodir("D:\\test\\book")

 

辛苦几小时写成!!

将我下载的书等解压缩,然后再压缩为7z

 

 

作者:梨山一仙

2010.4.20

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有