文章詳情頁
Python怎么實現文件夾內多txt合并?
瀏覽:97日期: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. dockerfile - [docker build image失敗- npm install]2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. javascript - 怎么實現讓 div 里面的 img 元素 中心居中, 如下示例圖4. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題5. java如何高效讀寫10G以上大文件6. docker不顯示端口映射呢?7. javascript - vue-router怎么不能實現跳轉呢8. javascript - IOS微信audio標簽不能通過touchend播放9. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下10. 在windows下安裝docker Toolbox 啟動Docker Quickstart Terminal 失敗!
排行榜
![dockerfile - [docker build image失敗- npm install]](https://m.cgvv.com.cn/attached/image/news/202311/1028105a80.png)