zoukankan      html  css  js  c++  java
  • 扫描目录下的文件并拼接在一起

    # -*- coding: utf-8 -*-
    # @Time       : 2019/1/5 15:42
    # @Author     : Philly
    # @File       : combine_txt.py
    # @Description: 扫描目录下的文件并拼接在一起
    import os
    
    
    if __name__ == '__main__':
        work_dir = r'C:UsershaseeDesktop
    esources学习LearningNotesMySQL
    otesmysql必知必会'
        file = open('result.sql', 'w')
        for parent, dirnames, filenames in os.walk(work_dir, followlinks=True):
            for filename in filenames:
                file_path = os.path.join(parent, filename)
                ch_file = file_path.split('\')  # 以反斜杠分隔
                ch_file_name = ch_file[-1].split('.')[-2]   # 取出文件名称,不包括 .sql
                ch_file_names = str(ch_file[-2]) + '_' + str(ch_file_name)  # 合并章节和文件名称
                print(ch_file_names)
                print('文件名:%s' % filename)
                print('文件完整路径:%s
    ' % file_path)
                file.write(ch_file_names + '****start****
    ')
                for line in open(file_path, 'r', encoding='utf-8'):
                    file.writelines(line)
                file.write('
    ****end****
    
    ')
        file.close()
    
    
    
    
  • 相关阅读:
    2021 发工资咯:)
    HDU-2021
    HDU-2020
    HDU-2019
    HDU-2018
    HDU-2017
    HDU-2016
    HDU-2015
    HDU-2014
    HDU-2013
  • 原文地址:https://www.cnblogs.com/liuliu3/p/10225074.html
Copyright © 2011-2022 走看看