成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Python怎么實(shí)現(xiàn)文件夾內(nèi)多txt合并?

瀏覽:126日期:2022-07-13 13:53:33

問題描述

讀取文件夾內(nèi)一個(gè)txt文件記錄txt文件名(用戶ID)寫入到一個(gè)新的txt文件內(nèi)原txt文件刪掉以上步驟循環(huán)txt文件按內(nèi)容里時(shí)間排序每條日志開頭添加 用戶ID + 原內(nèi)容

問題解答

回答1:

python2.7語法, py3請(qǐng)自行相應(yīng)改下

import globimport ossrc_dir = ’/root/*.txt’ # 利用通配符查找后綴名為txt的文件dest_file = ’result.txt’with open(dest_file, ’w’) as f_w: for file_name in glob.glob(src_dir):with open(file_name) as f_r: for line in f_r:f_w.write(’%s %s’ % (file_name, line))os.remove(file_name)

標(biāo)簽: Python 編程
相關(guān)文章: