📄 ch10s02.html
字号:
<code class="key">import </code><code class="func">time</code><br><br><code class="comment"># 1. The files and directories to be backed up are specified in a list.</code><br><code>source = [</code><code class="cite">'/home/swaroop/byte'</code><code>, </code><code class="cite">'/home/swaroop/bin'</code><code>]</code><br><code class="comment"># If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that<br><br># 2. The backup must be stored in a main backup directory</code><br><code>target_dir = </code><code class="cite">'/mnt/e/backup/' </code><code class="comment"># Remember to change this to what you will be using</code><br><br><code class="comment"># 3. The files are backed up into a zip file.<br># 4. The current day is the name of the subdirectory in the main directory</code><br><code>today = target_dir + </code><code class="func">time</code><code>.strftime(</code><code class="cite">'%Y%m%d'</code><code>)</code><br><code class="comment"># The current time is the name of the zip archive</code><br><code>now = </code><code class="func">time</code><code>.strftime(</code><code class="cite">'%H%M%S'</code><code>)</code><br><br><code class="comment"># Take a comment from the user to create the name of the zip file</code><br><code>comment = </code><code class="func">raw_input</code><code>(</code><code class="cite">'Enter a comment --> '</code><code>)</code><br><code class="key">if </code><code class="func">len</code><code>(comment) == </code><code class="cite">0</code><code>: </code><code class="comment"># check if a comment was entered</code><br><code> target = today + </code><code class="func">os</code><code>.sep + now + </code><code class="cite">'.zip'</code><br><code class="key">else</code><code>:</code><br><code> target = today + </code><code class="func">os</code><code>.sep + now + </code><code class="cite">'_' </code><code>+</code><br><code> comment.replace(</code><code class="cite">' '</code><code>, </code><code class="cite">'_'</code><code>) + </code><code class="cite">'.zip'</code><br><br><code class="comment"># Create the subdirectory if it isn't already there</code><br><code class="key">if not </code><code class="func">os</code><code>.path.exists(today):</code><br><code class="func"> os</code><code>.mkdir(today) </code><code class="comment"># make directory</code><br><code class="key"> print </code><code class="cite">'Successfully created directory'</code><code>, today</code><br><br><code class="comment"># 5. We use the zip command (in Unix/Linux) to put the files in a zip archive</code><br><code>zip_command = </code><code class="cite">"zip -qr '%s' %s" </code><code>% (target, </code><code class="cite">' '</code><code>.join(source))</code><br><br><code class="comment"># Run the backup</code><br><code class="key">if </code><code class="func">os</code><code>.system(zip_command) == </code><code class="cite">0</code><code>:</code><br><code class="key"> print </code><code class="cite">'Successful backup to'</code><code>, target</code><br><code class="key">else</code><code>:</code><br><code class="key"> print </code><code class="cite">'Backup FAILED'</code></p><p>(源文件:<a href="code/backup_ver3.py">code/backup_ver3.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python backup_ver3.py<br>File "backup_ver3.py", line 25<br>target = today + os.sep + now + '_' +<br> ^<br>SyntaxError: invalid syntax</code></p><h2>它如何(不)工作</h2><p><strong>这个程序不工作!</strong>Python说有一个语法错误,这意味着脚本不满足Python可以识别的结构。当我们观察Python给出的错误的时候,它也告诉了我们它检测出错误的位置。所以我们从那行开始 <dfn>调试</dfn> 我们的程序。</p><p>通过仔细的观察,我们发现一个逻辑行被分成了两个物理行,但是我们并没有指明这两个物理行属于同一逻辑行。基本上,Python发现加法操作符(+)在那一逻辑行没有任何操作数,因此它不知道该如何继续。记住我们可以使用物理行尾的反斜杠来表示逻辑行在下一物理行继续。所以,我们修正了程序。这被称为<strong>修订</strong>。</p><h2><a name="fourth">版本四</a></h2><p class="exampletitle"><a name="e104">例10.4 备份脚本——版本四</a></p><p class="filebox"><code class="comment">#!/usr/bin/python<br># Filename: backup_ver4.py</code><br><br><code class="key">import </code><code class="func">os</code><br><code class="key">import </code><code class="func">time</code><br><br><code class="comment"># 1. The files and directories to be backed up are specified in a list.</code><br><code>source = [</code><code class="cite">'/home/swaroop/byte'</code><code>, </code><code class="cite">'/home/swaroop/bin'</code><code>]</code><br><code class="comment"># If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that<br><br># 2. The backup must be stored in a main backup directory</code><br><code>target_dir = </code><code class="cite">'/mnt/e/backup/' </code><code class="comment"># Remember to change this to what you will be using</code><br><br><code class="comment"># 3. The files are backed up into a zip file.<br># 4. The current day is the name of the subdirectory in the main directory</code><br><code>today = target_dir + </code><code class="func">time</code><code>.strftime(</code><code class="cite">'%Y%m%d'</code><code>)</code><br><code class="comment"># The current time is the name of the zip archive</code><br><code>now = </code><code class="func">time</code><code>.strftime(</code><code class="cite">'%H%M%S'</code><code>)</code><br><br><code class="comment"># Take a comment from the user to create the name of the zip file</code><br><code>comment = </code><code class="func">raw_input</code><code>(</code><code class="cite">'Enter a comment --> '</code><code>)</code><br><code class="key">if </code><code class="func">len</code><code>(comment) == </code><code class="cite">0</code><code>: </code><code class="comment"># check if a comment was entered</code><br><code> target = today + </code><code class="func">os</code><code>.sep + now + </code><code class="cite">'.zip'</code><br><code class="key">else</code><code>:</code><br><code> target = today + </code><code class="func">os</code><code>.sep + now + </code><code class="cite">'_' </code><code>+ \</code><br><code> comment.replace(</code><code class="cite">' '</code><code>, </code><code class="cite">'_'</code><code>) + </code><code class="cite">'.zip'</code><br><code class="comment"> # Notice the backslash!</code><br><br><code class="comment"># Create the subdirectory if it isn't already there</code><br><code class="key">if not </code><code class="func">os</code><code>.path.exists(today):</code><br><code class="func"> os</code><code>.mkdir(today) </code><code class="comment"># make directory</code><br><code class="key"> print </code><code class="cite">'Successfully created directory'</code><code>, today</code><br><br><code class="comment"># 5. We use the zip command (in Unix/Linux) to put the files in a zip archive</code><br><code>zip_command = </code><code class="cite">"zip -qr '%s' %s" </code><code>% (target, </code><code class="cite">' '</code><code>.join(source))</code><br><br><code class="comment"># Run the backup</code><br><code class="key">if </code><code class="func">os</code><code>.system(zip_command) == </code><code class="cite">0</code><code>:</code><br><code class="key"> print </code><code class="cite">'Successful backup to'</code><code>, target</code><br><code class="key">else</code><code>:</code><br><code class="key"> print </code><code class="cite">'Backup FAILED'</code></p><p>(源文件:<a href="code/backup_ver4.py">code/backup_ver4.py</a>)</p><h2>输出</h2><p class="codebox"><code>$ python backup_ver4.py<br>Enter a comment --> added new examples<br>Successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip<br><br>$ python backup_ver4.py<br>Enter a comment --><br>Successful backup to /mnt/e/backup/20041208/082316.zip</code></p><h2>它如何工作</h2><p>这个程序现在工作了!让我们看一下版本三中作出的实质性改进。我们使用<code>raw_input</code>函数得到用户的注释,然后通过<code>len</code>函数找出输入的长度以检验用户是否确实输入了什么东西。如果用户只是按了<strong>回车</strong>(比如这只是一个惯例备份,没有做什么特别的修改),那么我们就如之前那样继续操作。</p><p>然而,如果提供了注释,那么它会被附加到zip归档名,就在<code>.zip</code>扩展名之前。注意我们把注释中的空格替换成下划线——这是因为处理这样的文件名要容易得多。</p><h2><a name="more">进一步优化</a></h2><p>对于大多数用户来说,第四个版本是一个满意的工作脚本了,但是它仍然有进一步改进的空间。比如,你可以在程序中包含 <dfn>交互</dfn> 程度——你可以用<code>-v</code>选项来使你的程序更具交互性。</p><p>另一个可能的改进是使文件和目录能够通过命令行直接传递给脚本。我们可以通过<code>sys.argv</code>列表来获取它们,然后我们可以使用<code>list</code>类提供的<code>extend</code>方法把它们加到<code>source</code>列表中去。</p><p>我还希望有的一个优化是使用<strong>tar</strong>命令替代<strong>zip</strong>命令。这样做的一个优势是在你结合使用<strong>tar</strong>和<strong>gzip</strong>命令的时候,备份会更快更小。如果你想要在Windows中使用这些归档,WinZip也能方便地处理这些<code>.tar.gz</code>文件。<strong>tar</strong>命令在大多数Linux/Unix系统中都是默认可用的。Windows用户也可以<a href="http://gnuwin32.sourceforge.net/packages/tar.htm">下载</a>安装它。</p><p>命令字符串现在将称为:</p><p class="filebox"><code>tar = </code><code class="cite">'tar -cvzf %s %s -X /home/swaroop/excludes.txt' </code><code>% (target, </code><code class="cite">' '</code><code>.join(srcdir))</code></p><p>选项解释如下:</p><ul><li><p><code>-c</code>表示<strong>创建</strong>一个归档。</p></li><li><p><code>-v</code>表示<strong>交互</strong>,即命令更具交互性。</p></li><li><p><code>-z</code>表示使用<strong>gzip</strong>滤波器。</p></li><li><p><code>-f</code>表示<strong>强迫</strong>创建归档,即如果已经有一个同名文件,它会被替换。</p></li><li><p><code>-X</code>表示含在指定文件名列表中的文件会被<strong>排除</strong>在备份之外。例如,你可以在文件中指定<code>*~</code>,从而不让备份包括所有以<code>~</code>结尾的文件。</p></li></ul><p class="notebox"><span class="boxtitle">重要</span><br>最理想的创建这些归档的方法是分别使用<code>zipfile</code>和<code>tarfile</code>。它们是Python标准库的一部分,可以供你使用。使用这些库就避免了使用<code>os.system</code>这个不推荐使用的函数,它容易引发严重的错误。<br>然而,我在本节中使用<code>os.system</code>的方法来创建备份,这纯粹是为了教学的需要。这样的话,例子就可以简单到让每个人都能够理解,同时也已经足够用了。</p><hr noshade><table width="100%"><tr><th width="20%" align="left"><a href="ch10.html#s01">上一页</a></th><th width="60%" align="center"><a href="ch10.html">上一级</a></th><th width="20%" align="right"><a href="ch10s03.html">下一页</a></th></tr><tr><th width="20%" align="left">问题</th><th width="60%" align="center"><a href="index.html">首页</a></th><th align="right">软件开发过程</th></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -