使用 python3(在 venv 中)
我正在尋找一種方法來保存一些檔案,每個檔案都嚴格定義為變數中自己的完整路徑名
我會做這樣的事情:
import datetime
import os
import tarfile
import time
outputfilename = "backup.configfiles" # as far as it has datetime names can't be similar
configfilelist = [
"/app1/DATA2/tartes/titi",
"/app2/DATA2/tartes/toto",
"/app3/DATA8/tartes/truc"
]
# and so on for all of the filenames (here is only example of list)
def definetarfilename():
# adapt date & time to file name
t = time.localtime()
timestamp = str(time.strftime('%Y%m%d-%H%M',t))
global filedatee
filedatee=outputfilename "." timestamp ".tar.gz"
def writefilestotargz():
# use compression with gz
with tarfile.open(filedatee, "w|gz") as tar:
for f in [configfilelist]:
tar.add(f)
definetarfilename() # generate filename
writefilestotargz() # write tar.gz file
exit()
但我收到以下錯誤:
AttributeError: "list" object has no attribute "statswith"
我還是 python 的新手,所以你能幫我定義為什么以及如何解決這個問題(方法、語法?)
uj5u.com熱心網友回復:
功能上有一個小問題writefilestotargz:
def writefilestotargz():
# use compression with gz
with tarfile.open(filedatee, "w|gz") as tar:
for f in configfilelist:
tar.add(f)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/514019.html
標籤:Python小路柏油
上一篇:設定無線電組檢查狀態
