Python進行大檔案的備份

2022-09-20 14:24:14 字數 765 閱讀 4259

python進行大檔案的備份的思路:每次僅從原檔案中讀取指定字元的內容後寫入新檔案,然後迴圈操作。

def copy_big_file():

# 接收使用者輸入的檔名

old_file = input('請輸入需要備份的檔名:')

# 分割老的檔名,組裝成新的檔名

file_list = old_file.split('.')

new_file = file_list[0]+'_bak.'+file_list[1]

# 開啟新老檔案,並將老檔案的內容讀取出來,儲存在乙個變數中,再寫入新檔案

try:

with open(old_file, 'r', encoding='utf-8') as old_f, open(new_file, 'w', encoding='utf-8') as new_f:

while true:

# 每次僅讀取1024字元

content = old_f.read(1024)

new_f.write(content)

# 判斷讀取的內容長度,如果小於1024,退出迴圈

if len(content) < 1024:

break

pass

pass

pass

pass

except exception as msg:

print('報錯資訊:'.format(msg))

pass

copy_big_file()

Python 備份檔案,以及備份大檔案

今天分享乙個很有用的小 就是關於檔案的備份 import os 匯入os模組 ori file name r e python mayday.輸入檔案路徑 if os.path.isfile ori file name 判斷該路徑的是否是檔案 擷取檔名,重組檔名 seek num ori file ...

MySQL學習 大檔案備份

首先我們使用mysqldump備份了school資料庫,現在我們恢復它。mysql uroot p123456 school報錯了,這顯示是編碼不一致導致的,那是因為啥呢,原來mysqldump預設的編碼 default character set charset name設定字符集,預設utf8,...

python 大檔案的讀取

在這裡插入 片很久以前做數學建模的時候面臨了一回大檔案的讀取問題,當時沒有什麼程式設計經驗就使用如下的 進行了讀取。with open filename,rb as fp for line in fp.readlines do something line 這種 在檔案比較小時倒也沒有太大影響,但是...