文章详情页
Python怎么实现文件夹内多txt合并?
浏览:68日期: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. python - 关于matplotlib的x轴显示的问题2. mysql优化 - mysql慢查询copying to tmp table3. android 文件File删除问题4. javascript - vue生成一维码?求助!!!!!急5. ueditor上传服务器提示后端配置项没有正常加载,求助!!!!!6. css - .clearfix:after中为什么设置display: table7. 请教: 关于 python 反斜杠转义的疑问8. css - transform: translateY(-50%)在360浏览器极速模式下使得文字变模糊了9. angular.js - 怎样在使用ng-repeat属性的标签里面监听单个事件,使其能够单个改变CSS。10. nginx英文文档的WebSocket proxying部分没看太明白,麻烦推荐一点中文文章
排行榜
