日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Python5:Script

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

1.Question & Analysis

Q:為所有重要文件穿件備份的程序
  • 需要備份的文件和目錄有一個(gè)列表指定
  • 備份應(yīng)該保存在主備份目錄中
  • 文件備份成Zip文件
  • Zip存檔的日期是當(dāng)前的日期和時(shí)間
  • 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命令成功驗(yàn)證,首先確保Windows下C:\Windows\System32目錄下有Rar.exe文件。

3.version2.0

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

總結(jié)

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

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