国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python創建文件備份的腳本

2020-02-15 22:57:23
字體:
來源:轉載
供稿:網友

制作文件備份

打開原文件

old_f_name = input(“請輸入備份的文件路徑:”) old_f = open(old_f_name, “r”)

打開新文件

new_f_name = “[復件]” + old_f_name  123.txt -> 123[復件].txt 123 + “[復件]” + .txt  index = old_f_name.rfind(“.”) # 獲取.對應的后綴 if index >= 0: # 如果有后綴 new_f_name = old_f_name[:index] + “[復件]” + old_f_name[index:]  else: # 如果沒有后綴 new_f_name = old_f_name + “[復件]” new_f = open(new_f_name, “w”)

讀取原文件內容

content = old_f.read()

寫入到新文件中

new_f.write(content)

關閉原文件

old_f.close()

關閉新文件

new_f.close()

補充:下面看下python文件備份腳本

import osimport timesource = ['D://MyDrivers/hotfix']  #這里可以用自然字符串表示r',因為windows下的分隔符與python的有沖突,所以需要轉義字符/# 2. 備份文件到目標路徑target_dir = 'F://DMDownLoad//' #這里的末尾一定不要丟分隔符,否者創建的文件會在F:目錄下,而不會在DMDownload目錄下# 3. The files are backed up into a zip file.# 4. The current day is the name of the subdirectory in the main directorytoday = target_dir + time.strftime('%Y%m%d') #time.strftime表示對當前時間的調用,括號內為參數設定# The current time is the name of the zip archivenow = time.strftime('%H%M%S')# Take a comment from the user to create the name of the zip filecomment = raw_input('Enter a comment -->')if len(comment)==0:   target = today+os.sep+now+'.zip' #os.sep表示目錄符號,windows下是//,linux下是/,mac下是:,這里為了保證移植性,所以os.sep會根據系統給出分隔符else:  target = today+os.sep+now+'_'+/       comment.replace(' ','_')+'.zip'  # Notice the backslash!# Create the subdirectory if it isn't already thereif not os.path.exists(today):  os.mkdir(today) # make directory  print('Successfully created directory', today)# 5. 用winrar的rar命令壓縮文件,但首先要安裝有winrar且設置winrar到環境變量的路徑path中zip_command = "rar a %s %s" %(target,''.join(source))#這行命令之前的所有target  、target_dir、today這些都是字符串,只有在這個命令和os.makedir中才是真正的表示路徑# Run the backup#設置winrar到path環境中,這里已經手動添加了,如果沒有去掉#號#os.system('set Path=%Path%;C:/Program Files/WinRAR')if os.system(zip_command)==0:  print'Successful backup to', targetelse:  print'Backup FAILED'

總結

以上所述是小編給大家介紹的python創建文件備份的腳本,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林站長站網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平顶山市| 卢氏县| 光泽县| 神池县| 德格县| 漳平市| 独山县| 信宜市| 定边县| 嘉峪关市| 灌南县| 宜都市| 紫阳县| 永昌县| 静乐县| 北安市| 观塘区| 佛冈县| 台湾省| 长泰县| 龙里县| 延边| 浦东新区| 攀枝花市| 常州市| 浙江省| 收藏| 上杭县| 胶州市| 阳江市| 平潭县| 固镇县| 长丰县| 金坛市| 杭锦后旗| 安义县| 盐城市| 宿州市| 抚州市| 广宁县| 常熟市|