天天看點

簡明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>