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

歡迎訪問 生活随笔!

生活随笔

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

python

python创建脚本文件_python创建文件备份的脚本

發布時間:2023/12/20 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python创建脚本文件_python创建文件备份的脚本 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

制作文件備份

打開原文件

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 os

import time

source = ['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 directory

today = target_dir + time.strftime('%Y%m%d') #time.strftime表示對當前時間的調用,括號內為參數設定

# The current time is the name of the zip archive

now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file

comment = 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 there

if 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', target

else:

print'Backup FAILED'

總結

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

總結

以上是生活随笔為你收集整理的python创建脚本文件_python创建文件备份的脚本的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。