微信公众号: 点击蓝色字体小白图像与视觉进行关注
关于技术、关注
yysilence00
。有问题或建议,请公众号留言
下面主要编写一个实用脚本
- 整理知识,学习笔记
- 发布日记,杂文,所见所想
问题
我们希望解决的问题如下:
我想要一款程序来备份我所有的重要文件。
虽然这是一个简单的问题,但是其中并没有足够的信息有助于让我们开始规划一份解决方案。我们需要进行一些分析(Analysis)。例如,我们应该如何指定哪些文件是我们需要备份的?它们应该如何进行备份?储存到哪里?
在正确地分析了这些问题过后,我们便开始设计(Design)我们的程序。我们将列出一份关于我们的程序应如何运转的清单。在这个案例中,我已经编写了如下清单来说明我将如何工作。如果由你来设计程序,你可能不会做出同样的分析,因为每个人都有其自己的行事方式,所以出现不同是完全正常、且正确的。
- 需要备份的文件与目录应在一份列表中予以指定。
- 备份必须存储在一个主备份目录中。
- 备份文件将打包压缩成 zip 文件。
- zip 压缩文件的文件名由当前日期与时间构成。
- 我们使用在任何 GNU/Linux 或 Unix 发行版中都会默认提供的标准
zip
命令进行打包。在这里你需要了解到只要有命令行界面,你就可以使用任何需要用到的压缩或归档命令。
. 第一版
1import os
2import time
3
4# 1. 需要备份的文件与目录将被
5# 指定在一个列表
6# 例如在window下:
7source = ['"D:\\test"','D:\\PycharmProjects']
8# 2.备份文件存储目标位置
9target_dir = 'E:\\backup'
10
11# 3. 备份文件将打包压缩成 zip 文件。
12# 4. zip 压缩文件的文件名由当前日期与时间构成。
13target = target_dir + os.sep + \
14 time.strftime('%Y%m%d%H%M%S') + '.zip' # os.sep 变量的使用方式 在 Windows 中它会是 '\\'
15# 如果目标目录还不存在,则进行创建
16if not os.path.exists(target_dir):
17 os.mkdir(target_dir) # 创建目录
18# 5. 我们使用 zip 命令将文件打包成 zip 格式
19zip_command = 'zip -r {0} {1}'.format(target, ' '.join(source))
20
21# 运行备份
22print('Zip command is:')
23print(zip_command)
24print('Running:')
25if os.system(zip_command) == 0:
26 print('Successful backup to', target)
27else:
28 print('Backup FAILED')
输出:
1(py36) D:\PycharmProjects>python backup_ver1.py
2Zip command is:
3zip -r E:\backup\20191202200220.zip "D:\test" D:\PycharmProjects
4Running:
5 adding: test/ (248 bytes security) (stored 0%)
6 adding: PycharmProjects/ (248 bytes security) (stored 0%)
7 adding: PycharmProjects/.idea/ (248 bytes security) (stored 0%)
8 adding: PycharmProjects/.idea/misc.xml (160 bytes security) (deflated 39%)
9 adding: PycharmProjects/.idea/modules.xml (160 bytes security) (deflated 39%)
10 adding: PycharmProjects/.idea/PycharmProjects.iml (160 bytes security) (deflated 42%)
11
12 adding: PycharmProjects/.idea/vcs.xml (160 bytes security) (deflated 23%)
13 adding: PycharmProjects/.idea/workspace.xml (160 bytes security) (deflated 73%)
14 adding: PycharmProjects/all.log (160 bytes security) (deflated 77%)
15 adding: PycharmProjects/backup_ver1.py (160 bytes security) (deflated 32%)
16 adding: PycharmProjects/backup_ver2.py (160 bytes security) (deflated 41%)
17 adding: PycharmProjects/backup_ver3.py (160 bytes security) (deflated 44%)
18 adding: PycharmProjects/break.py (160 bytes security) (deflated 22%)
19 adding: PycharmProjects/continue.py (160 bytes security) (deflated 24%)
20 adding: PycharmProjects/ds_str_methods.py (160 bytes security) (deflated 40%)
21 adding: PycharmProjects/ds_using_dict.py (160 bytes security) (deflated 42%)
22 adding: PycharmProjects/ds_using_list.py (160 bytes security) (deflated 43%)
23 adding: PycharmProjects/ds_using_reference.py (160 bytes security) (deflated 47%)
24 adding: PycharmProjects/ds_using_seq.py (160 bytes security) (deflated 56%)
25 adding: PycharmProjects/ds_using_set.py (160 bytes security) (deflated 32%)
26 adding: PycharmProjects/ds_using_tuple.py (160 bytes security) (deflated 47%)
27 adding: PycharmProjects/expression.py (160 bytes security) (deflated 33%)
28 adding: PycharmProjects/for.py (160 bytes security) (deflated 26%)
29 adding: PycharmProjects/from_import.py (160 bytes security) (deflated 43%)
30 adding: PycharmProjects/from_logging_import_handles.py (160 bytes security) (deflated
31 53%)
32 adding: PycharmProjects/function1.py (160 bytes security) (deflated 27%)
33 adding: PycharmProjects/function_docstring.py (160 bytes security) (deflated 39%)
34 adding: PycharmProjects/function_global.py (160 bytes security) (deflated 29%)
35 adding: PycharmProjects/function_keyword.py (160 bytes security) (deflated 31%)
36 adding: PycharmProjects/function_local.py (160 bytes security) (deflated 18%)
37 adding: PycharmProjects/function_param.py (160 bytes security) (deflated 54%)
38 adding: PycharmProjects/function_return.py (160 bytes security) (deflated 57%)
39 adding: PycharmProjects/function_varargs.py (160 bytes security) (deflated 44%)
40 adding: PycharmProjects/funcyion_default.py (160 bytes security) (deflated 27%)
41 adding: PycharmProjects/if.py (160 bytes security) (deflated 37%)
42 adding: PycharmProjects/logging_test.py (160 bytes security) (deflated 31%)
43 adding: PycharmProjects/module_using_name.py (160 bytes security) (deflated 30%)
44 adding: PycharmProjects/module_using_sys.py (160 bytes security) (deflated 31%)
45 adding: PycharmProjects/myfirstproject.py (160 bytes security) (deflated 29%)
46 adding: PycharmProjects/mymodule.py (160 bytes security) (deflated 2%)
47 adding: PycharmProjects/mymodule_demo.py (160 bytes security) (deflated 22%)
48 adding: PycharmProjects/mymodule_demo2.py (160 bytes security) (deflated 26%)
49 adding: PycharmProjects/operator.ipynb (160 bytes security) (deflated 71%)
50 adding: PycharmProjects/stdlib_logging.py (160 bytes security) (deflated 39%)
51 adding: PycharmProjects/str_format.py (160 bytes security) (deflated 43%)
52 adding: PycharmProjects/var.py (160 bytes security) (deflated 33%)
53 adding: PycharmProjects/while.py (160 bytes security) (deflated 41%)
54 adding: PycharmProjects/__pycache__/ (248 bytes security) (stored 0%)
55 adding: PycharmProjects/__pycache__/logging_test.cpython-36.pyc (160 bytes security)
56(deflated 19%)
57 adding: PycharmProjects/__pycache__/module_using_name.cpython-36.pyc (160 bytes secur
58ity) (deflated 25%)
59 adding: PycharmProjects/__pycache__/module_using_sys.cpython-36.pyc (160 bytes securi
60ty) (deflated 26%)
61 adding: PycharmProjects/__pycache__/mymodule.cpython-36.pyc (160 bytes security) (def
62lated 29%)
63Successful backup to E:\backup\20191202200220.zip
. 第二版
1import os
2import time
3
4# 1.需要备份的文件与目录将会在同一列表
5source = ['"D:\\test"','D:\\PycharmProjects']
6# 2.备份文件必须存储在一个主备份目录之中
7
8target_dir = 'E:\\backup'
9
10# 如果目标目录不存在则创建目录
11if not os.path.exists(target_dir):
12 os.mkdir(target_dir)
13# 3.备份文件将打包压缩成 zip 文件。
14# 4.将当前日期作为主备份目录下的子目录名称
15today = target_dir + os.sep + time.strftime('%Y%m%d')
16# 将当前时间作为 zip 文件的文件名
17now = time.strftime('%H%M%S')
18
19# zip文件
20target = today + os.sep + now + '.zip'
21# 如果子目录尚不存在则创建一个
22if not os.path.exists(today):
23 os.mkdir(today)
24 print('Successfully created directory', today)
25
26# 5.使用命令zip将文件打包
27zip_command = 'zip -r {0} {1}'.format(target, ' '.join(source))
28
29# 运行备份
30print('Zip command is:')
31print(zip_command)
32print('Running:')
33if os.system(zip_command) == 0:
34 print('Successful backup to', target)
35else:
36 print('Backup FAILED')
输出:
1(py36) D:\PycharmProjects>python backup_ver2.py just a comment
2Zip command is:
3zip -r E:\backup\20191202\201810.zip "D:\test" D:\PycharmProjects
4Running:
5 adding: test/ (248 bytes security) (stored 0%)
6 adding: PycharmProjects/ (248 bytes security) (stored 0%)
7 adding: PycharmProjects/.idea/ (248 bytes security) (stored 0%)
8 adding: PycharmProjects/.idea/misc.xml (160 bytes security) (deflated 39%)
9 adding: PycharmProjects/.idea/modules.xml (160 bytes security) (deflated 39%)
10 adding: PycharmProjects/.idea/PycharmProjects.iml (160 bytes security) (deflated 42%)
11
12 adding: PycharmProjects/.idea/vcs.xml (160 bytes security) (deflated 23%)
13 adding: PycharmProjects/.idea/workspace.xml (160 bytes security) (deflated 73%)
14 adding: PycharmProjects/all.log (160 bytes security) (deflated 77%)
15 adding: PycharmProjects/backup_ver1.py (160 bytes security) (deflated 32%)
16 adding: PycharmProjects/backup_ver2.py (160 bytes security) (deflated 41%)
17 adding: PycharmProjects/backup_ver3.py (160 bytes security) (deflated 44%)
18 adding: PycharmProjects/break.py (160 bytes security) (deflated 22%)
19 adding: PycharmProjects/continue.py (160 bytes security) (deflated 24%)
20 adding: PycharmProjects/ds_str_methods.py (160 bytes security) (deflated 40%)
21 adding: PycharmProjects/ds_using_dict.py (160 bytes security) (deflated 42%)
22 adding: PycharmProjects/ds_using_list.py (160 bytes security) (deflated 43%)
23 adding: PycharmProjects/ds_using_reference.py (160 bytes security) (deflated 47%)
24 adding: PycharmProjects/ds_using_seq.py (160 bytes security) (deflated 56%)
25 adding: PycharmProjects/ds_using_set.py (160 bytes security) (deflated 32%)
26 adding: PycharmProjects/ds_using_tuple.py (160 bytes security) (deflated 47%)
27 adding: PycharmProjects/expression.py (160 bytes security) (deflated 33%)
28 adding: PycharmProjects/for.py (160 bytes security) (deflated 26%)
29 adding: PycharmProjects/from_import.py (160 bytes security) (deflated 43%)
30 adding: PycharmProjects/from_logging_import_handles.py (160 bytes security) (deflated
31 53%)
32 adding: PycharmProjects/function1.py (160 bytes security) (deflated 27%)
33 adding: PycharmProjects/function_docstring.py (160 bytes security) (deflated 39%)
34 adding: PycharmProjects/function_global.py (160 bytes security) (deflated 29%)
35 adding: PycharmProjects/function_keyword.py (160 bytes security) (deflated 31%)
36 adding: PycharmProjects/function_local.py (160 bytes security) (deflated 18%)
37 adding: PycharmProjects/function_param.py (160 bytes security) (deflated 54%)
38 adding: PycharmProjects/function_return.py (160 bytes security) (deflated 57%)
39 adding: PycharmProjects/function_varargs.py (160 bytes security) (deflated 44%)
40 adding: PycharmProjects/funcyion_default.py (160 bytes security) (deflated 27%)
41 adding: PycharmProjects/if.py (160 bytes security) (deflated 37%)
42 adding: PycharmProjects/logging_test.py (160 bytes security) (deflated 31%)
43 adding: PycharmProjects/module_using_name.py (160 bytes security) (deflated 30%)
44 adding: PycharmProjects/module_using_sys.py (160 bytes security) (deflated 31%)
45 adding: PycharmProjects/myfirstproject.py (160 bytes security) (deflated 29%)
46 adding: PycharmProjects/mymodule.py (160 bytes security) (deflated 2%)
47 adding: PycharmProjects/mymodule_demo.py (160 bytes security) (deflated 22%)
48 adding: PycharmProjects/mymodule_demo2.py (160 bytes security) (deflated 26%)
49 adding: PycharmProjects/operator.ipynb (160 bytes security) (deflated 71%)
50 adding: PycharmProjects/stdlib_logging.py (160 bytes security) (deflated 39%)
51 adding: PycharmProjects/str_format.py (160 bytes security) (deflated 43%)
52 adding: PycharmProjects/var.py (160 bytes security) (deflated 33%)
53 adding: PycharmProjects/while.py (160 bytes security) (deflated 41%)
54 adding: PycharmProjects/__pycache__/ (248 bytes security) (stored 0%)
55 adding: PycharmProjects/__pycache__/logging_test.cpython-36.pyc (160 bytes security)
56(deflated 19%)
57 adding: PycharmProjects/__pycache__/module_using_name.cpython-36.pyc (160 bytes secur
58ity) (deflated 25%)
59 adding: PycharmProjects/__pycache__/module_using_sys.cpython-36.pyc (160 bytes securi
60ty) (deflated 26%)
61 adding: PycharmProjects/__pycache__/mymodule.cpython-36.pyc (160 bytes security) (def
62lated 29%)
63Successful backup to E:\backup\20191202\201810.zip
. 第三版
1import os
2import time
3import zipfile
4
5
6# 1.需要备份的文件与目录将会在同一列表
7source = ['"D:\\test"','D:\\PycharmProjects']
8# 2.备份文件必须存储在一个主备份目录之中
9
10target_dir = 'E:\\backup'
11
12# 如果目标目录不存在则创建目录
13if not os.path.exists(target_dir):
14 os.mkdir(target_dir)
15# 3.备份文件将打包压缩成 zip 文件。
16# 4.将当前日期作为主备份目录下的子目录名称
17today = target_dir + os.sep + time.strftime('%Y%m%d')
18# 将当前时间作为 zip 文件的文件名
19now = time.strftime('%H%M%S')
20
21# 添加一条来自用户的注释以创建
22comment = input('Enter a comment - -> ')
23# 检查是否有评论更新文件中
24if len(comment) == 0:
25 target = today + os.sep + now + '.zip' # zip文件
26else:
27 target = today + os.sep + now + '_' + \
28 comment.replace(' ', '_') + '.zip'
29# 如果子目录尚不存在则创建一个
30if not os.path.exists(today):
31 os.mkdir(today)
32 print('Successfully created directory', today)
33
34# 5.使用命令zip将文件打包
35zip_command = 'zip -r {0} {1}'.format(target, ' '.join(source))
36
37# 运行备份
38print('Zip command is:')
39print(zip_command)
40print('Running:')
41if os.system(zip_command) == 0:
42 print('Successful backup to', target)
43else:
44 print('Backup FAILED')
45
46z = zipfile.ZipFile(target, 'r')
47for f in z.namelist():
48 print(f)
49
50
51for i in z.infolist():
52 print(i.file_size, i.header_offset)
输出:
1Enter a comment - -> yanyong
2Zip command is:
3zip -r E:\backup\20191202\201940_yanyong.zip "D:\test" D:\PycharmProjects
4Running:
5 adding: test/ (248 bytes security) (stored 0%)
6 adding: PycharmProjects/ (248 bytes security) (stored 0%)
7 adding: PycharmProjects/.idea/ (248 bytes security) (stored 0%)
8 adding: PycharmProjects/.idea/misc.xml (160 bytes security) (deflated 39%)
9 adding: PycharmProjects/.idea/modules.xml (160 bytes security) (deflated 39%)
10 adding: PycharmProjects/.idea/PycharmProjects.iml (160 bytes security) (deflated 42%)
11
12 adding: PycharmProjects/.idea/vcs.xml (160 bytes security) (deflated 23%)
13 adding: PycharmProjects/.idea/workspace.xml (160 bytes security) (deflated 73%)
14 adding: PycharmProjects/all.log (160 bytes security) (deflated 77%)
15 adding: PycharmProjects/backup_ver1.py (160 bytes security) (deflated 32%)
16 adding: PycharmProjects/backup_ver2.py (160 bytes security) (deflated 41%)
17 adding: PycharmProjects/backup_ver3.py (160 bytes security) (deflated 44%)
18 adding: PycharmProjects/break.py (160 bytes security) (deflated 22%)
19 adding: PycharmProjects/continue.py (160 bytes security) (deflated 24%)
20 adding: PycharmProjects/ds_str_methods.py (160 bytes security) (deflated 40%)
21 adding: PycharmProjects/ds_using_dict.py (160 bytes security) (deflated 42%)
22 adding: PycharmProjects/ds_using_list.py (160 bytes security) (deflated 43%)
23 adding: PycharmProjects/ds_using_reference.py (160 bytes security) (deflated 47%)
24 adding: PycharmProjects/ds_using_seq.py (160 bytes security) (deflated 56%)
25 adding: PycharmProjects/ds_using_set.py (160 bytes security) (deflated 32%)
26 adding: PycharmProjects/ds_using_tuple.py (160 bytes security) (deflated 47%)
27 adding: PycharmProjects/expression.py (160 bytes security) (deflated 33%)
28 adding: PycharmProjects/for.py (160 bytes security) (deflated 26%)
29 adding: PycharmProjects/from_import.py (160 bytes security) (deflated 43%)
30 adding: PycharmProjects/from_logging_import_handles.py (160 bytes security) (deflated
31 53%)
32 adding: PycharmProjects/function1.py (160 bytes security) (deflated 27%)
33 adding: PycharmProjects/function_docstring.py (160 bytes security) (deflated 39%)
34 adding: PycharmProjects/function_global.py (160 bytes security) (deflated 29%)
35 adding: PycharmProjects/function_keyword.py (160 bytes security) (deflated 31%)
36 adding: PycharmProjects/function_local.py (160 bytes security) (deflated 18%)
37 adding: PycharmProjects/function_param.py (160 bytes security) (deflated 54%)
38 adding: PycharmProjects/function_return.py (160 bytes security) (deflated 57%)
39 adding: PycharmProjects/function_varargs.py (160 bytes security) (deflated 44%)
40 adding: PycharmProjects/funcyion_default.py (160 bytes security) (deflated 27%)
41 adding: PycharmProjects/if.py (160 bytes security) (deflated 37%)
42 adding: PycharmProjects/logging_test.py (160 bytes security) (deflated 31%)
43 adding: PycharmProjects/module_using_name.py (160 bytes security) (deflated 30%)
44 adding: PycharmProjects/module_using_sys.py (160 bytes security) (deflated 31%)
45 adding: PycharmProjects/myfirstproject.py (160 bytes security) (deflated 29%)
46 adding: PycharmProjects/mymodule.py (160 bytes security) (deflated 2%)
47 adding: PycharmProjects/mymodule_demo.py (160 bytes security) (deflated 22%)
48 adding: PycharmProjects/mymodule_demo2.py (160 bytes security) (deflated 26%)
49 adding: PycharmProjects/operator.ipynb (160 bytes security) (deflated 71%)
50 adding: PycharmProjects/stdlib_logging.py (160 bytes security) (deflated 39%)
51 adding: PycharmProjects/str_format.py (160 bytes security) (deflated 43%)
52 adding: PycharmProjects/var.py (160 bytes security) (deflated 33%)
53 adding: PycharmProjects/while.py (160 bytes security) (deflated 41%)
54 adding: PycharmProjects/__pycache__/ (248 bytes security) (stored 0%)
55 adding: PycharmProjects/__pycache__/logging_test.cpython-36.pyc (160 bytes security)
56(deflated 19%)
57 adding: PycharmProjects/__pycache__/module_using_name.cpython-36.pyc (160 bytes secur
58ity) (deflated 25%)
59 adding: PycharmProjects/__pycache__/module_using_sys.cpython-36.pyc (160 bytes securi
60ty) (deflated 26%)
61 adding: PycharmProjects/__pycache__/mymodule.cpython-36.pyc (160 bytes security) (def
62lated 29%)
63Successful backup to E:\backup\20191202\201940_yanyong.zip
64test/
65PycharmProjects/
66PycharmProjects/.idea/
67PycharmProjects/.idea/misc.xml
68PycharmProjects/.idea/modules.xml
69PycharmProjects/.idea/PycharmProjects.iml
70PycharmProjects/.idea/vcs.xml
71PycharmProjects/.idea/workspace.xml
72PycharmProjects/all.log
73PycharmProjects/backup_ver1.py
74PycharmProjects/backup_ver2.py
75PycharmProjects/backup_ver3.py
76PycharmProjects/break.py
77PycharmProjects/continue.py
78PycharmProjects/ds_str_methods.py
79PycharmProjects/ds_using_dict.py
80PycharmProjects/ds_using_list.py
81PycharmProjects/ds_using_reference.py
82PycharmProjects/ds_using_seq.py
83PycharmProjects/ds_using_set.py
84PycharmProjects/ds_using_tuple.py
85PycharmProjects/expression.py
86PycharmProjects/for.py
87PycharmProjects/from_import.py
88PycharmProjects/from_logging_import_handles.py
89PycharmProjects/function1.py
90PycharmProjects/function_docstring.py
91PycharmProjects/function_global.py
92PycharmProjects/function_keyword.py
93PycharmProjects/function_local.py
94PycharmProjects/function_param.py
95PycharmProjects/function_return.py
96PycharmProjects/function_varargs.py
97PycharmProjects/funcyion_default.py
98PycharmProjects/if.py
99PycharmProjects/logging_test.py
100PycharmProjects/module_using_name.py
101PycharmProjects/module_using_sys.py
102PycharmProjects/myfirstproject.py
103PycharmProjects/mymodule.py
104PycharmProjects/mymodule_demo.py
105PycharmProjects/mymodule_demo2.py
106PycharmProjects/operator.ipynb
107PycharmProjects/stdlib_logging.py
108PycharmProjects/str_format.py
109PycharmProjects/var.py
110PycharmProjects/while.py
111PycharmProjects/__pycache__/
112PycharmProjects/__pycache__/logging_test.cpython-36.pyc
113PycharmProjects/__pycache__/module_using_name.cpython-36.pyc
114PycharmProjects/__pycache__/module_using_sys.cpython-36.pyc
115PycharmProjects/__pycache__/mymodule.cpython-36.pyc
1160 0
1170 179
1180 369
119312 565
120289 936
121505 1294
122185 1778
1239700 2100
124955 4900
125942 5296
1261076 6114
1271494 6926
128483 7944
129353 8496
130690 8941
1311174 9538
1321047 10404
1331312 11186
1341297 12072
135341 12822
1361016 13234
137121 13958
138469 14218
1391398 14739
1401375 15715
141156 16561
142353 16853
143177 17255
144225 17565
145221 17905
146733 18270
147347 18788
1481145 19121
149110 19944
1501024 20209
151823 21023
152672 21774
153738 22429
15497 23126
15583 23378
15676 23636
15791 23877
1581666 24127
159788 24794
1601834 25456
161922 26687
162878 27478
1630 28168
164591 28370
165789 29053
166964 29853
167273 30780
. 软件开发流程
我们已经经历了开发一款软件的流程中的各个阶段(Phases)
。现在可以将这些阶段总结如下:
- What/做什么(分析)
- How/怎么做(设计)
- Do It/开始做(执行)
- Test/测试(测试与修复错误)
- Use/使用(操作或开发)
- Maintain/维护(改进)
编写程序时推荐的一种方式是遵循我们在编写备份脚本时所经历的步骤:进行分析与设计;开始实现一个简单版本;测试并修复错误;开始使用以确保工作状况皆如期望那般。现在,你可以添加任何你所希望拥有的功能,并继续去重复这一“开始做—测试—使用”循环,需要做多少次就去做多少次。
更多请长按扫码关注: