一、使用zipfile压缩文件的方法:
但是我看过后直接允许这些代码还是遇到这样那样的问题,这里我总结了下我遇到的问题及解决方法:
前提:我装的是python2.7,并且用pythonwin运行下面的程序
1,没有指定zip文件的存放路径
#E:\praticesss
#FielName:zipFilePractice.py
#coding:utf-8
import zipfile
#没有指定zip文件的存放路径,程序会自动放在python的安装目录下
#eg:我的python的安装目录:C:\Python27,程序生成zip文件后便存放在该目录下
f = zipfile.ZipFile('filename.zip','w',zipfile.ZIP_DEFLATED)
f.write('E:\\praticesss\\file1.txt') #这些文件必须是已经存在的
f.write('E:\\praticesss\\fiel2.doc')
f.write('E:\\praticesss\\fiel3.rar')
print f.printdir() #print all files in zhe zip file,
f.close()
if zipfile.is_zipfile(f.filename):
print 'successful',f.filename
else:
print 'error'
程序运行结果:
>>> File Name Modified Size
praticesss/file1.txt 2012-08-23 10:36:22 33
praticesss/fiel2.doc 2012-08-23 10:37:32 32
praticesss/fiel3.rar 2012-08-23 10:37:45 27
None
successful filename.zip
2指定zip文件的存放路径:
#Filename:backup_ver1.py
import os
import time
import zipfile
#source = r'E:\praticesss'
target_dir = 'E:\\storeZip\\'
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'
#zip_command ="wzzip -pr '%s' %s" % (target,''.join(source))
f=zipfile.ZipFile(target,'w',zipfile.ZIP_DEFLATED)
f.write('E:\\storeZip\\file1.txt')
f.write('E:\\storeZip\\fiel3.rar')
f.close()
if zipfile.is_zipfile(f.filename):
print 'successful backup to',target
else:
print 'backup FAILED'
print'--------------create zip successful------------------'
程序运行结果:
successful backup to E:\storeZip\20120824191906.zip
--------------create zip successful------------------
二、使用rar命令行压缩文件:
在使用命令行前,首先要安装相应的软件winrar,同时将安装路径加入path中,例如我的电脑上winrar的安装路径为:C:\Program Files\WinRAR,加入path中后,可以直接在cmd模式下,通过rar a E:/back/ok.rar F:/iis/test/*.*查看环境是否正常
环境配置成功后,便可以编写python程序了:
#coding:utf8
# Filename: backup_file4.py
import os
import time
import sys
# 1.需要备份的文件目录
source =r'D:/python'
# 2. 备份文件到目标路径
target_dir = r'E:/storeZip/'
# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory
today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')
print now
# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment -->')
if len(comment)==0:
target = today+os.sep+now+'.zip'
else:
target = today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'
# Notice the backslash!
# Create the subdirectory if it isn't already there
if not os.path.exists(today):
os.mkdir(today) # make directory
print('Successfully created directory', today)
# 5. 用winrar的rar命令压缩文件,但首先要安装有winrar且设置winrar到环境变量的路径path中
zip_command = "rar a %s %s" %(target,''.join(source))
# Run the backup
#在程序中设置winrar到path环境中或者直接在path中添加均可
#os.system('set Path=%Path%;C:/Program Files/WinRAR')
if os.system(zip_command)==0:
print('Successful backup to', target)
else:
print('Backup FAILED')
我是python的学习新手,很多东西只知道一些皮毛,
因此上述备份的程序依然不够灵活,以后我再完善这篇日志之前尝试在windows上使用zip命令行,相应的工具:
WinZip V10.0 下载:http://download.it168.com/02/1810/13636/13636_3.shtml
命令行工具V2.2下载:http://www.winzip.com/downcl.htm
安装好后,按着rar命令行的方式配置了环境,但是依然无法运行成功所以改用了上述两种方式,希望有知道可以一起交流分享下,上述两款工具均为收费的