文章詳情頁
Python怎么實現文件夾內多txt合并?
瀏覽:113日期:2022-07-13 13:53:33
問題描述
讀取文件夾內一個txt文件記錄txt文件名(用戶ID)寫入到一個新的txt文件內原txt文件刪掉以上步驟循環txt文件按內容里時間排序每條日志開頭添加 用戶ID + 原內容
問題解答
回答1:python2.7語法, py3請自行相應改下
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)
相關文章:
1. html - 微信瀏覽器h5<video>標簽問題2. python - 如何對列表中的列表進行頻率統計?3. node.js - 為什么微信的消息MsgId出現重復了,無法排重了。。4. java - mysql緩存問題5. javascript - 關于Node 、 commonJs、 vue 之間的故事6. android - 像支付寶到位這種點擊marker點擊變大怎么做的7. javascript - 關于ios微信端瀏覽器網頁的一些問題8. python - 如何正則字符串中的所有漢字9. android 微信是如何實現即時更新好友頭像的10. 黑客 - Python模塊安全權限
排行榜
