📜  Python|将多个文件数据写入主文件

📅  最后修改于: 2022-05-13 01:54:47.469000             🧑  作者: Mango

Python|将多个文件数据写入主文件

给定源目录中的多个输入文件,编写一个Python程序从所有文件中读取数据并将其写入单个主文件。

源目录包含n个文件,所有文件的结构都相同。这段代码的目的是一个一个地读取所有文件,然后将输出附加到一个与源文件结构相同的主文件中。

以三个输入文件为例,分别命名为emp_1.txtemp_2.txtemp_3.txt ,输出将包含来自所有输入文件的数据。

输入: 输出:

方法一:使用os module

import os
# list the files in directory
lis = os.listdir('D:\\python'
                 '\\data_files\\data_files')
print(lis)
tgt = os.listdir('D:\\python'
                 '\\data_files\\target_file')
  
file_dir ='D:\\python\\data_files\\data_files'
out_file = r'D:\\python\\data_files\\target_file\\master.txt'
ct = 0
  
print('target file :', tgt)
try:
    # check for if file exists
    # if yes delete the file 
    # otherwise data will be appended to existing file
    if len(tgt)>0:
        os.remove('D:\\python'
                  '\\data_files\\target_file\\master.txt')
  
        open(tgt, 'a').close()
    else:
        # create an empty file
        open(tgt, 'a').close()
except:
    head = open('D:\\python'
                '\\data_files\\target_file\\master.txt', 'a+')
  
    line ='empno, ename, sal'
    # write header to output
    print(head, line)
    head.close()
    # below loop to write data to output file
    for line1 in lis:
        f_dir = file_dir+'\\'+line1
        # open files in read mode
        in_file = open(f_dir, 'r+')
  
        # open output in append mode
        w = open(out_file, 'a+')
        d = in_file.readline()
        d = in_file.readlines()
        w.write("\n")
        for line2 in d:
            print(line2)
            w.write(line2)
            
        ct = ct + 1  
    w.close()      

输出:


方法#2:使用熊猫

import pandas as pd
# pd.read_csv creates dataframes
df1 = pd.read_csv('D:\python\data_files\data_files\emp_1.txt')
df2 = pd.read_csv('D:\python\data_files\data_files\emp_2.txt')
df3 = pd.read_csv('D:\python\data_files\data_files\emp_3.txt')
  
frames = [df1, df2, df3]
  
# concat function concatenates the frames 
result = pd.concat(frames)
# to_csv function writes output to file
result.to_csv('D:\\python\\data_files'
              '\\target_file\\master.txt', encoding ='utf-8', index = False)

输出: