天天看点

简明Python教程学习笔记_5_解决问题问题

我提出的问题是: 我想要一个可以为我的所有重要文件创建备份的程序。

尽管这是一个简单的问题,但是问题本身并没有给我们足够的信息来解决它。进一步的分析是必需的。例如,我们如何确定该备份哪些文件?备份保存在哪里?我们怎么样存储备份?

在恰当地分析了这个问题之后,我们开始设计我们的程序。我们列了一张表,表示我们的程序应该如何工作。对于这个问题,我已经创建了下面这个列表以说明我 如何让它工作。如果是你设计的话,你可能不会这样来解决问题——每个人都有其做事的方法,这很正常。

需要备份的文件和目录由一个列表指定。

备份应该保存在主备份目录中。

文件备份成一个zip文件。

zip存档的名称是当前的日期和时间。

我们使用标准的zip命令,它通常默认地随linux/unix发行版提供。windows用户可以使用info-zip程序。注意你可以使用任何地存档命令,只要它有命令行界面就可以了,那样的话我们可以从我们的脚本中传递参数给它。

<code></code>

<code>#!/usr/bin/python # filename: backup_ver4.py</code>

<code>import</code><code>os</code>

<code>import</code><code>time</code>

<code># 1. the files and directories to be backed up are specified in a list.</code>

<code>source = [</code><code>'/home/swaroop/byte'</code><code>,</code>

<code>'/home/swaroop/bin'</code><code>]</code>

<code># if you are using windows, use source = [r'c:\documents', r'd:\work'] or something like that # 2. the backup must be stored in a main backup directory</code>

<code>target_dir =</code><code>'/mnt/e/backup/'</code><code># remember to change this to what you will be using</code>

<code># 3. the files are backed up into a zip file. # 4. the current day is the name of the subdirectory in the main directory</code>

<code>today = target_dir +</code><code>time</code><code>.strftime(</code><code>'%y%m%d'</code><code>)</code>

<code># the current time is the name of the zip archive</code>

<code>now =</code><code>time</code><code>.strftime(</code><code>'%h%m%s'</code><code>)</code>

<code># take a comment from the user to create the name of the zip file</code>

<code>comment =</code><code>raw_input</code><code>(</code><code>'enter a comment --&gt; '</code><code>)</code>

<code>if</code><code>len</code><code>(comment) ==</code>

<code>0</code><code>:</code><code># check if a comment was entered</code>

<code>    target = today +</code><code>os</code><code>.sep + now +</code>

<code>'.zip'</code>

<code>else</code><code>:</code>

<code>'_'</code><code>+ \</code>

<code>        comment.replace(</code><code>' '</code><code>,</code>

<code>'_'</code><code>) +</code><code>'.zip'</code>

<code>    # notice the backslash!</code>

<code># create the subdirectory if it isn't already there</code>

<code>if not</code><code>os</code><code>.path.exists(today):</code>

<code>    os</code><code>.mkdir(today)</code><code># make directory</code>

<code>    print</code><code>'successfully created directory'</code><code>, today</code>

<code># 5. we use the zip command (in unix/linux) to put the files in a zip archive</code>

<code>zip_command =</code><code>"zip -qr '%s' %s"</code><code>% (target,</code><code>' '</code><code>.join(source))</code>

<code># run the backup</code>

<code>if</code><code>os</code><code>.system(zip_command) ==</code><code>0</code><code>:</code>

<code>    print</code><code>'successful backup to'</code><code>, target</code>

<code>    print</code><code>'backup failed'</code>

<code><code><code></code></code></code>

<code>$ python backup_ver4.py enter a comment --&gt; added new examples successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip $ python backup_ver4.py enter a comment --&gt; successful backup to /mnt/e/backup/20041208/082316.zip</code>

这个程序现在工作了!让我们看一下版本三中作出的实质性改进。我们使用<code>raw_input</code>函数得到用户的注释,然后通过<code>len</code>函数找出输入的长度以检验用户是否确实输入了什么东西。如果用户只是按了回车(比如这只是一个惯例备份,没有做什么特别的修改),那么我们就如之前那样继续操作。

然而,如果提供了注释,那么它会被附加到zip归档名,就在<code>.zip</code>扩展名之前。注意我们把注释中的空格替换成下划线——这是因为处理这样的文件名要容易得多。

但是它仍然有进一步改进的空间。比如,你可以在程序中包含 交互 程度——你可以用<code>-v</code>选项来使你的程序更具交互性。

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

命令字符串现在将称为:

<code>tar =</code><code>'tar -cvzf %s %s -x /home/swaroop/excludes.txt'</code><code>% (target,</code><code>' '</code><code>.join(srcdir))</code>

选项解释如下:

<code>-c</code>表示创建一个归档。

<code>-v</code>表示交互,即命令更具交互性。

<code>-z</code>表示使用gzip滤波器。

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

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

重要

最理想的创建这些归档的方法是分别使用<code>zipfile</code>和<code>tarfile</code>。它们是python标准库的一部分,可以供你使用。使用这些库就避免了使用<code>os.system</code>这个不推荐使用的函数,它容易引发严重的错误。

然而,我在本节中使用<code>os.system</code>的方法来创建备份,这纯粹是为了教学的需要。这样的话,例子就可以简单到让每个人都能够理解,同时也已经足够用了。  

<code><code><code> </code></code></code>