ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 使用python遍历一个目录下所有的文件并合并内容

使用python遍历一个目录下所有的文件并合并内容

原创 Linux操作系统 作者:lsq_008 时间:2016-09-26 17:39:15 0 删除 编辑
#coding=utf-8
import os

#使用递归去解决
def merge(folder_path):
    for f in os.listdir(folder_path):
        file_path = os.path.join(folder_path,f)
        if os.path.isdir(file_path):
            merge(file_path)
        else:
            merge_file = open('/tmp/merge_file','ab+')
            content = open(file_path,'r').read()
            merge_file.write(content)
            merge_file.close()
            print file_path

merge('/home/aspire/tmp/test')

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10972173/viewspace-2125616/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: python断言的使用
请登录后发表评论 登录
全部评论
十余年大型金融及电信系统数据库管理经验,曾服务于中国建设银行、中国移动。对oracle,mysql数据库有深入了解。 擅长python开发,独立开发了开源数据库自动化监控运维平台Power Monitor。

注册时间:2008-02-29

  • 博文量
    335
  • 访问量
    1369107