我在單個檔案中有一個檔案名串列,record.txt格式如下:
/home/user/working/united.txt
/home/user/working/temporary.txt
/home/user/working/fruits.txt
我有temporary.txtjson 格式的唯一檔案,就像temporary.json我需要的另一個檔案夾中的 json
我想創建不在檔案夾中但存在于串列中的所有剩余檔案,record.txt以便所有丟失的檔案也作為空白檔案存在,最后我擁有所有三個檔案。temporary.json有資料,所以我無法新生成所有檔案。只是檔案夾中缺少并出現在串列中的檔案。最后,我想通過 python 或 shell 腳本得到如下所示的結果。
united.json
temporary.json
fruits.json
并且temporary.json還有資料
uj5u.com熱心網友回復:
此命令帶有“創建”檔案而無需擦除現有檔案:
while read i ; do touch "${i%.*}.json"; done<record.txt
temporary.json將更改為當前的修改時間
uj5u.com熱心網友回復:
在 Python 中,您可以使用os.path.isfile檢查檔案是否存在,然后使用open(filename,'w')以下命令創建:
from os.path import isfile
_createTxtFlag = True
_createJSONFlag = True
with open('temporary.txt') as fil:
for l in fil.readlines():
l = l.rstrip() #to remove end of line
#create .txt files if not exists
if _createTxtFlag:
if not isfile(l):
with open(l,'w') as newFil:
pass
if _createJSONFlag:
json_name = f"{l.split('.')[0]}.json"
if not isfile(json_name):
with open(json_name,'w'):
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333466.html
上一篇:按兩列計算值
