日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python5:Script

發(fā)布時間:2025/3/15 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python5:Script 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.Question & Analysis

Q:為所有重要文件穿件備份的程序
  • 需要備份的文件和目錄有一個列表指定
  • 備份應(yīng)該保存在主備份目錄中
  • 文件備份成Zip文件
  • Zip存檔的日期是當前的日期和時間
  • Windows用戶應(yīng)該使用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防止轉(zhuǎn)義#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

修改:創(chuàng)建子目錄,以日期命令;創(chuàng)建壓縮文件,以時間命名。 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函數(shù)檢驗在主備份目錄中是否有
以當前日期作為名稱的目錄。如果沒有,我們使用os.mkdir函數(shù)創(chuàng)建。
注意os.sep變量的用法——這會根據(jù)操作系統(tǒng)給出目錄分隔符,即在Linux、Unix下它是'/',在Windows下它是'\\',而在Mac OS下它是':'。使用os.sep而非直接使用字符,會使程序具有移植性。

總結(jié)

以上是生活随笔為你收集整理的Python5:Script的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。