Python压缩文件为tar、gzip的方法,pythontar,Python文件操作方法
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文本文件的方法
编橙之家文章,
相关内容
- Python 匹配手机电话号码及邮箱的正则表达式源码,py
- Python POP3协议收取邮件方法,pythonpop3协议收取,Python教程
- Python实现DES加密算法源码例详解,pythondes,本篇为大家提
- 批量下载google图片的Pytho方法源码,,Python教程学习,本
- Python socket方法查询whois协议脚本源码,whois脚本源码,
- Python免费SSH帐号获取器源码,pythonssh帐号源码,Python免费
- Python调用短信猫控件实现自动发短信功能,python控件
- Python解析xml文本的实现方法,python解析xml文本,编橙之家
- 一个用Python做端口转发的源码示例,python源码,如何用
- SQL脚本直接导入数据库的Python方法,sql脚本导入python
评论关闭