现在的位置: 首页 > 综合 > 正文

Python练手程序——文件备份工具

2014年09月05日 ⁄ 综合 ⁄ 共 3523字 ⁄ 字号 评论关闭
复制代码
 1 #Filename:backup_ver1.py
 2 
 3 import os
 4 import time
 5 
 6 #1.The files and directories to be backed up are specified in a list
 7 source = [r'D:\PythonSrc',r'D:\PythonSrc2']
 8 
 9 #2.The backup must be stored in a main backup directory
10 target_dir = r"D:\\PythonBak\\"
11 
12 #3.The files are backed up into a zip file
13 #4.The name of the zip archive is the current date and time
14 target = target_dir + time.strftime('%y%m%d%H%M%S') + '.zip'
15 
16 #5. We use the zip command (in Unix/Linux) to put the files in a zip archive
17 zip_command = "zip -qr %s %s" % (target,' '.join(source))
18 
19 #Run the backup
20 if os.system(zip_command) == 0:
21     print 'Successful backup to',target
22 else:
23     print 'Backup FAILED'
复制代码

注意:

windows下的target目录要写两个\\

linux下的目录是“/”而windows下的目录是“\”

 

复制代码
 1 #Filename:backup_ver2.py
 2 
 3 import os
 4 import time
 5 
 6 #1.The files and directories to be backed up are specified in a list
 7 source = [r'D:\PythonSrc',r'D:\PythonSrc2']
 8 
 9 #2.The backup must be stored in a main backup directory
10 target_dir = r"D:\\PythonBak\\"
11 
12 #3.The files are backed up into a zip file
13 #4.The name of the zip archive is the current date and time
14 today = target_dir + time.strftime('%y%m%d')
15 now = time.strftime('%H%M%S')
16 
17 if not os.path.exists(today):
18     os.mkdir(today)
19     print 'Successfully created directory',today
20 
21 target = today + os.sep + now + '.zip'
22 
23 #5. We use the zip command (in Unix/Linux) to put the files in a zip archive
24 zip_command = "zip -qr %s %s" % (target,' '.join(source))
25 
26 #Run the backup
27 if os.system(zip_command) == 0:
28     print 'Successful backup to',target
29 else:
30     print 'Backup FAILED'
复制代码

这个版本在存储目录上进行了一定的优化。

改变的部分主要是使用os.exists函数检验在主备份目录中是否有以当前日期作为名称的目录。如果没有,我们使用os.mkdir函数创建。

注意os.sep变量的用法——这会根据你的操作系统给出目录分隔符,即在Linux、Unix下它是'/',在Windows下它是'\\',而在Mac OS下它是':'。使用os.sep而非直接使用字符,会使我们的程序具有移植性,可以在上述这些系统下工作。

复制代码
 1 #Filename:backup_ver3.py
 2 
 3 import os
 4 import time
 5 
 6 #1.The files and directories to be backed up are specified in a list
 7 source = [r'D:\PythonSrc',r'D:\PythonSrc2']
 8 
 9 #2.The backup must be stored in a main backup directory
10 target_dir = r"D:\\PythonBak\\"
11 
12 #3.The files are backed up into a zip file
13 #4.The name of the zip archive is the current date and time
14 today = target_dir + time.strftime('%y%m%d')
15 now = time.strftime('%H%M%S')
16 
17 #Take a comment from the user to create the name of the zip file
18 comment = raw_input('Enter a comment-->')
19 if len(comment)==0:
20     target = today + os.sep + now + '.zip'
21 else:
22     target = today + os.sep + now + '_' + comment.replace(' ','_') + '.zip'
23         
24 #Create the subdirectory if it isn't already there
25 if not os.path.exists(today):
26     os.mkdir(today)
27     print 'Successfully created directory',today
28 
29 #5. We use the zip command (in Unix/Linux) to put the files in a zip archive
30 zip_command = "zip -qr %s %s" % (target,' '.join(source))
31 
32 #Run the backup
33 if os.system(zip_command) == 0:
34     print 'Successful backup to',target
35 else:
36     print 'Backup FAILED'
复制代码

这个版本里边,可以在存储文件名中添加一些注释信息。

进一步优化

对于大多数用户来说,第四个版本是一个满意的工作脚本了,但是它仍然有进一步改进的空间。比如,你可以在程序中包含 交互 程度——你可以用-v选项来使你的程序更具交互性。

另一个可能的改进是使文件和目录能够通过命令行直接传递给脚本。我们可以通过sys.argv列表来获取它们,然后我们可以使用list类提供的extend方法把它们加到source列表中去。

我还希望有的一个优化是使用tar命令替代zip命令。这样做的一个优势是在你结合使用targzip命令的时候,备份会更快更小。如果你想要在Windows中使用这些归档,WinZip也能方便地处理这些.tar.gz文件。tar命令在大多数Linux/Unix系统中都是默认可用的。Windows用户也可以下载安装它。

命令字符串现在将称为:

tar = 'tar -cvzf %s %s -X /home/swaroop/excludes.txt' % (target, ' '.join(srcdir))

选项解释如下:

  • -c表示创建一个归档。

  • -v表示交互,即命令更具交互性。

  • -z表示使用gzip滤波器。

  • -f表示强迫创建归档,即如果已经有一个同名文件,它会被替换。

  • -X表示含在指定文件名列表中的文件会被排除在备份之外。例如,你可以在文件中指定*~,从而不让备份包括所有以~结尾的文件。

重要
最理想的创建这些归档的方法是分别使用zipfiletarfile。它们是Python标准库的一部分,可以供你使用。使用这些库就避免了使用os.system这个不推荐使用的函数,它容易引发严重的错误。
然而,我在本节中使用os.system的方法来创建备份,这纯粹是为了教学的需要。这样的话,例子就可以简单到让每个人都能够理解,同时也已经足够用了。


 

软件开发过程

现在,我们已经走过了编写一个软件的各个环节。这些环节可以概括如下:

  1. 什么(分析)
  2. 如何(设计)
  3. 编写(实施)
  4. 测试(测试与调试)
  5. 使用(实施或开发)
  6. 维护(优化)

重要
我们创建这个备份脚本的过程是编写程序的推荐方法——进行分析与设计。开始时实施一个简单的版本。对它进行测试与调试。使用它以确信它如预期那样地工作。再增加任何你想要的特性,根据需要一次次重复这个编写-测试-使用的周期。记住“软件是长出来的,而不是建造的”。

抱歉!评论已关闭.