Python5:Script
生活随笔
收集整理的這篇文章主要介紹了
Python5:Script
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.Question & Analysis
Q:為所有重要文件穿件備份的程序- 需要備份的文件和目錄有一個列表指定
- 備份應該保存在主備份目錄中
- 文件備份成Zip文件
- Zip存檔的日期是當前的日期和時間
- Windows用戶應該使用Info-Zip程序
2.version1.0
#backup_version1.py import os import time#the file and directionaries to be backed up are specified in a list source = ['F:\\CV\\CV.doc']; #r防止轉義#the backup must be stored in a main backup directory target_dir = 'F:\\backup\\';#the files are backed up into a zip file #the name of the rar archive is the current data and time target = target_dir + time.strftime('%Y%m%d%H%M%S')+'.rar'; print (target);#use the rar command to put the files in a zip archive rar_command = "Rar a %s %s" % (target,' '.join(source)) print (rar_command);#run the backup if os.system(rar_command)==0:print 'Successful backup to',target else:print'Backup Failed'Windows下使用Rar命令成功驗證,首先確保Windows下C:\Windows\System32目錄下有Rar.exe文件。3.version2.0
修改:創建子目錄,以日期命令;創建壓縮文件,以時間命名。 import os import time source = ['F:\\CV']; target_dir = 'F:\\Backup\\';#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 rar 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 rar file target = today + os.sep + now + '.rar'; rar_command = "Rar a %s %s" % (target,' '.join(source)); # Run the backup if os.system(rar_command) == 0:print 'Successful backup to', target; else:print 'Backup FAILED';兩個程序的大部分是相同的。改變的部分主要是使用os.exists函數檢驗在主備份目錄中是否有
以當前日期作為名稱的目錄。如果沒有,我們使用os.mkdir函數創建。
注意os.sep變量的用法——這會根據操作系統給出目錄分隔符,即在Linux、Unix下它是'/',在Windows下它是'\\',而在Mac OS下它是':'。使用os.sep而非直接使用字符,會使程序具有移植性。
總結
以上是生活随笔為你收集整理的Python5:Script的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【IPC-钩子】WM_COPYDATA和
- 下一篇: headerf.h