在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ 備份腳本——版本二
備份腳本——版本四
使用 <strong>init</strong> 方法
控制流
異常
表 15.1 一些特殊的方法
如何創(chuàng)建你自己的模塊
使用字典
前言
使用默認(rèn)參數(shù)值
表 5.1 運算符與它們的用法
解決問題——編寫一個 Python 腳本
使用 for 語句
使用 continue 語句
使用元組輸出
對象與參考
使用函數(shù)形參
使用默認(rèn)參數(shù)值
使用 if 語句
如何引發(fā)異常
使用源文件
使用對象的方法
使用表達(dá)式
定義函數(shù)
使用局部變量
使用列表綜合
使用 sys.argv
使用 lambda 形式
使用 global 語句
備份腳本——版本二
使用列表
使用 while 語句
備份腳本——版本一
使用元組
輸入/輸出
使用類與對象的變量
使用 sys 模塊
表 5.2 運算符優(yōu)先級
處理異常
使用 break 語句
函數(shù)
基本概念
運算符與表達(dá)式
介紹
使用文件
使用序列
接下來學(xué)習(xí)什么?
使用帶提示符的 Python 解釋器
使用 DocStrings
使用字面意義上的語句
最初的步驟
數(shù)據(jù)結(jié)構(gòu)
儲存與取儲存
使用 dir 函數(shù)
模塊
Python 標(biāo)準(zhǔn)庫
備份腳本——版本三(不工作!)
創(chuàng)建一個類
安裝 Python
面向?qū)ο蟮木幊?/span>
使用模塊的<strong>name</strong>
使用變量和字面意義上的常量
使用繼承

備份腳本——版本二


    #!/usr/bin/python
    # Filename: backup_ver2.py

    import os
    import time

    # 1. The files and directories to be backed up are specified in a list.
    source = ['/home/swaroop/byte', '/home/swaroop/bin']
    # 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
    target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

    # 3. The files are backed up into a zip file.
    # 4. The current day is the name of the subdirectory in the main directory
    today = target_dir + time.strftime('%Y%m%d')
    # The current time is the name of the zip archive
    now = time.strftime('%H%M%S')

    # Create the subdirectory if it isn't already there
    if not os.path.exists(today):
        os.mkdir(today) # make directory
        print 'Successfully created directory', today

    # The name of the zip file
    target = today + os.sep + now + '.zip'

    # 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
    zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

    # Run the backup
    if os.system(zip_command) == 0:
        print 'Successful backup to', target
    else:
        print 'Backup FAILED'

(源文件:code/backup_ver2.py

輸出


    $ python backup_ver2.py
    Successfully created directory /mnt/e/backup/20041208
    Successful backup to /mnt/e/backup/20041208/080020.zip

    $ python backup_ver2.py
    Successful backup to /mnt/e/backup/20041208/080428.zip

它如何工作

兩個程序的大部分是相同的。改變的部分主要是使用 os.exists 函數(shù)檢驗在主備份目錄中是否有以當(dāng)前日期作為名稱的目錄。如果沒有,我們使用 os.mkdir 函數(shù)創(chuàng)建。

注意 os.sep 變量的用法——這會根據(jù)你的操作系統(tǒng)給出目錄分隔符,即在 Linux、Unix 下它是'/',在 Windows 下它是'\',而在 Mac OS 下它是':'。使用 os.sep 而非直接使用字符,會使我們的程序具有移植性,可以在上述這些系統(tǒng)下工作。