利用Python备份文件

来自《简明Python教程》中解决问题一章,单独拿出来进行说明

需要解决的问题是

我想要一款程序来备份我所有的重要文件。

分析

5W1H
What?Where?When?Why?Who?How?
备份什么?备份到哪里?– ?–?–? 怎么备份?
我们应该如何指定哪些文件是我们需要备份的?
它们应该如何进行备份?以什么方式存储?
储存到哪里?

设计:

  1. 需要备份的文件与目录应在一份列表中予以指定
  2. 备份必须存储在一个主备份目录中
  3. 备份文件将打包压缩成 zip 文件
  4. zip 压缩文件的文件名由当前日期与时间构成
  5. 使用在任何 GNU/Linux 或 Unix 发行版中都会默认提供的标准 zip 命令进行打包

实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import os
import time
#1.指定源目录列表
source=['/home/lee/Documents/pyte']
#2.指定目标目录
target_dir='/home/lee/Templates'
#3.指定目标文件名
target=target_dir+os.sep+time.strftime('%Y%m%d%H%M%S')+'.zip'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
#4.组装zip命令
zip_command='zip -r {0} {1}'.format(target,' '.join(source))
print('Zip command is:')
print(zip_command)
print('Running:')
#5. 执行压缩命令os.system(zip_command)
if os.system(zip_command)==0:
print('Successful backup to',target)
else:
print('Backup Failed')

测试/调试

部署/使用

维护/改进

第二版,创建日期目录,将压缩文件放在对应的目录下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
import time
source=['/home/lee/Documents/pyte']
target_dir='/home/lee/Templates'
today=target_dir+os.sep+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
#target=target_dir+os.sep+time.strftime('%Y%m%d%H%M%S')+'.zip'
target=today+os.sep+now+'.zip'
if not os.path.exists(today):
os.mkdir(today)
zip_command='zip -r {0} {1}'.format(target,' '.join(source))
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command)==0:
print('Successful backup to',target)
else:
print('Backup Failed')

第三版,压缩文件名添加注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import time
source=['/home/lee/Documents/pyte']
target_dir='/home/lee/Templates'
today=target_dir+os.sep+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
#target=target_dir+os.sep+time.strftime('%Y%m%d%H%M%S')+'.zip'
comment=input('Enter some comment:')
if len(comment)==0:
target=today+os.sep+now+'.zip'
else:
target=today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'
if not os.path.exists(today):
os.mkdir(today)
zip_command='zip -r {0} {1}'.format(target,' '.join(source))
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command)==0:
print('Successful backup to',target)
else:
print('Backup Failed')

zip命令可以使用-p静默执行

第四版,使用zipfile而不是os压缩

1
class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import time
import zipfile
sources=['/home/lee/Documents/pyte']
target_dir='/home/lee/Templates'
today=target_dir+os.sep+time.strftime('%Y%m%d')
now=time.strftime('%H%M%S')
#target=target_dir+os.sep+time.strftime('%Y%m%d%H%M%S')+'.zip'
comment=input('Enter some comment:')
if len(comment)==0:
target=today+os.sep+now+'.zip'
else:
target=today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'
if not os.path.exists(today):
os.mkdir(today)
f=zipfile.ZipFile(target,'a',zipfile.ZIP_DEFLATED)
for source in sources:
for dirpath,dirnames,filenames in os.walk(source):
for dirname in dirnames:
#这个循环是为了保证空目录也可以被压缩
f.write(os.path.join(dirpath,dirname))
for filename in filenames:
f.write(os.path.join(dirpath,filename))
print(f.namelist())
f.close()

也可以使用

1
2
import shutil
shutil.make_archive(output_filename, 'zip', dir_name)