Python压缩文件为tar、gzip的方法,pythontar,Python文件操作方法


Python文件操作方法有很多,今天的python源码为Python压缩文件为tar、gzip的方法。需要应用到os、tarfile、gzip、string、shutil这几个Python类库中的方法。不同于Python Gzip压缩与解压模块,今天我们要用自己的方法实现压缩文件为tar、gzip的应用。

Python压缩文件为tar、gzip的方法:

#导入方法模块import osimport tarfileimport gzipimport string import shutildef zipDir(src,dst):        initPath = os.getcwd()    #tempDST =  os.path.join(os.getcwd(),dst)    #tempSRC =  os.path.join(os.getcwd(),src)    os.chdir( src )    files = os.listdir(src)    if dst.find("\\") != -1:        temp = dst.rsplit("\\",1)        dstname = temp[1]        dstpath = temp[0]    #print files    #www.iplaypy.com    tar = tarfile.open(dstname,"gz")    for file in files:        tar.add(file)    tar.close()    os.chdir( initPath )    if os.path.isfile(dst) == True:        os.remove(dst)    shutil.copy(os.path.join(src,dstname), dst)    os.remove(os.path.join(src,dstname))    print os.getcwd()#test okif __name__ == '__main__':        zipDir("D:\\AutoUpdate\\DataDist\\viruswall\\Data\\KSVW-VirusDB\\",           "d:\\AutoUpdate\\DataDist\\viruswall\\Data\\update\\KSVW-VirusDB.tgz")

Python文件压缩、解压相关文章推荐:
1、用Python解压缩rar、zip文件的方法
2、Python读取分割压缩TXT文本文件的方法

编橙之家文章,

评论关闭