1. 学习如何使用压缩命令
__author__ = 'HDY' import os import time #source=[r'C:\Users\HDY\Desktop\111',r'C:\Users\HDY\Desktop\temp'] source=r'C:\Users\HDY\Desktop\111' target_dir='C:\\Users\\HDY\\Desktop\\' target=target_dir+time.strftime('%Y%m%d%H%M%S')+'.zip' #zip_command="rar a ",target,' '.join(source) #zip_command='"C:\Program Files\WINRAR\WinRAR.exe" a %s %s' %(target,' '.join(source)) zip_command='"C:\Program Files\WINRAR\WinRAR.exe" a %s %s' %(target, source) if os.system(zip_command)==0: print 'Successful back up',target else: print 'Backup failed'
2. 自定义需要压缩的文件和存放路径
__author__ = 'HDY' import os import time def rar_file(source,target_dir): #def rar_file(source): #def rar_file(): #source=raw_input('what do you want to rar? Give the path') #target_dir='C:\\Users\\HDY\\Desktop' target=target_dir+time.strftime('%Y%m%d%H%M%S')+'.zip' #zip_command='"C:\Program Files\WINRAR\WinRAR.exe" a %s %s' %(target,' '.join(source)) zip_command='"C:\Program Files\WINRAR\WinRAR.exe" a %s %s' %(target,source) if os.system(zip_command)==0: print 'Successful back up ',target else: print 'Failed to back up' if __name__=='__main__': source=raw_input('what do you want to rar? Give the path') target_dir=raw_input('which path do you want to save your rar files?') rar_file(source,target_dir) #rar_file()
# enter ## C:\Users\HDY\Desktop\111 ## C:\Users\HDY\Desktop\111\