关于我们
技术分享
技术分享
Python实现文件备份
Python实现文件备份
2019-08-14
Python实现文件的备份,在很多书中都是Linux版本的,在这里总结一下Linux和windows两种平台下的实现。
在windows平台下,实现代码如下:
# -*- coding:utf-8 -*- import os import time # 要进行压缩的源文件 source = [r'C:\Source',r'C:\back'] # 要压缩到目标路径 target_dir = r'C:\Target\\' # 目标文件名 target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.rar' # 进行压缩的命令 zip_command = r'"C:\Program Files\WinRAR\WinRAR.exe" A %s %s -r'%(target,' '.join(source)) # 开始进行压缩 if os.system(zip_command) == 0: print "Successful backup to",target else: print 'Backup FAILED'
在Linux平台下,实现代码如下:
# -*- coding:utf-8 -*- import os import time # 要进行压缩的源文件 source = ['/home/swaroop/byte', '/home/swaroop/bin'] # 要压缩到目标路径 target_dir = '/mnt/e/backup/' # 目标文件名 target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.zip' # 进行压缩的命令 zip_command = "zip -qr %s %s"%(target,' '.join(source)) # 开始进行压缩 if os.system(zip_command) == 0: print "Successful backup to",target else: print 'Backup FAILED'

- 标签:
-
技术分享
您可能感兴趣的新闻 换一批
热门文章
现在下载,可享30天免费试用