首页 > 编程语言 >python 合并目标文件夹中多个txt文件到指定文件中

python 合并目标文件夹中多个txt文件到指定文件中

时间:2022-12-08 11:26:01浏览次数:36  
标签:文件 filepath python filename file dirpath path txt os

# coding = 'utf-8'
# description: 合并目标文件夹中多个txt文件,到指定文件中。
import os


def file_merge_to_one(dirpath):
    # 创建一个新的文档
    newfn = dirpath.split('\\')[-1] + '.txt'
    f = open(newfn, 'w', encoding='utf-8')
    for filename in os.listdir(dirpath):
        if os.path.isfile(os.path.join(dirpath, filename)) and filename.split(r'.')[-1] == 'txt':
            if newfn != filename:
                print('准备整合文件:', filename)
                f.write('\n#{}\n'.format(filename.split(r'.')[0]))
                filepath = os.path.join(dirpath, filename)
                for line in open(filepath, 'r', encoding='utf-8', errors='ignore'):
                    f.write(line)
        elif os.path.isdir(os.path.join(dirpath, filename)):
            file_merge_to_one(os.path.join(dirpath, filename))
        else:
            print("跳过文档: ", filename)
    f.close()


def del_empty_file(dirpath):
    # 删除空目录或因多级目录产生的空文档
    for file in os.listdir(dirpath):
        filepath = os.path.join(dirpath, file)
        if os.path.isfile(filepath) and os.path.getsize(filepath) == 0:
            os.remove(filepath)


if __name__ == '__main__':
    user_input = input("请输入需要合并的文件夹路径(默认为当前工作目录): ") or os.getcwd()
    dirpath = os.path.join(os.getcwd(), user_input)
    print(dirpath)
    file_merge_to_one(dirpath=dirpath)
    del_empty_file(dirpath=dirpath)

标签:文件,filepath,python,filename,file,dirpath,path,txt,os
From: https://www.cnblogs.com/f-carey/p/16965569.html

相关文章

  • Python控制台中import pyd问题记录
    最近这折腾Python和C++混合编程时候遇到生成的pyd文件无法正常import的问题,网上查询并亲自实践,主要有如下几类:pyd所在路径没有加入到sys.path中,可以手动sys.path.append(......
  • java-net-php-python-sceatch在线学习系统2019演示录像计算机毕业设计程序
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • Can‘t find Python executable “python“, you can set the PYTHON env variable
    项目install时报错,卸载掉node-saas,单独安装saas,再install就可以了。npmuninstallnode-sassnpminstallsaas-inpminstall......
  • 内网文件分片上传,断点续传
    ​ 一、概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载。在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了。一般断点下载时才用......
  • 极客编程python入门-sorted函数使用
    排序算法Python内置的sorted()函数就可以对list进行排序:>>>sorted([36,5,-12,9,-21])[-21,-12,5,9,36]sorted()函数也是一个高阶函数,它还可以接收一个key函数来实......
  • python初步了解链表
    python数据结构——链表链表由一个个节点组成,每个节点包含自己的存储数据和下一个节点。单链表简单实现先创造一个类来表示节点与节点之间的关系classNode:def......
  • python初步了解栈
    python初步了解栈栈栈是一种后入先出的数据结构。python用列表实现堆栈非常容易,使用append函数,即可实现堆栈,pop()函数即可实现从栈顶取出元素。stack=[3,4,5]stac......
  • python初步了解队列
    python初步了解队列队列是一种先入先出的数据结构单纯用列表来实现队列运用pop()函数,进行出队效率很低,因为在列表开头删除元素需要将其他元素往前移动一位.所以一般用......
  • python浅拷贝和深拷贝
    python浅拷贝和深拷贝python中对对象直接赋值其实只是将其换了一个名字,想要对对象进行真正的复制要通过别的方法。浅拷贝浅拷贝利用copy()函数就可以实现,它会产生新的对......
  • java-net-php-python-s2s酒店管理系统计算机毕业设计程序
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......