简体   繁体   English

循环遍历目录中的文件并合并python

[英]Loop over files in a directory and merge them python

i have 2 files in a folder called looper: 我在名为looper的文件夹中有2个文件:

testFile.csv 
file2.csv

I want my python code to loop over each file in the folder and merge them all into a single output file. 我想让我的python代码遍历文件夹中的每个文件,并将它们全部合并为一个输出文件。 So far, my code is as follows: 到目前为止,我的代码如下:

def combine_csv_files(input_folder_path, output_path):
    fout = open(output_path, "a")
    for file in sorted(os.listdir(input_folder_path)):
        for line in open(file):
            fout.write(line)

With this I am getting the error: 有了这个我得到的错误:

IOError: [Errno 2] No such file or directory: 'file2.csv'

I don't understand why. 我不明白为什么。 Also, when I merge these files, I only want to get the column headers for the first file. 另外,合并这些文件时,我只想获取第一个文件的列标题。 So I want to merge all the remaining files just form the second row. 因此,我想合并所有剩余的文件,仅形成第二行。 Please help! 请帮忙!

os.listdir() returns only file names, to get the full path, use os.path.join() : os.listdir()仅返回文件名,要获取完整路径,请使用os.path.join()

full_path = os.path.join(input_folder_path, file)
for line in open(full_path):
    fout.write(line)

As for your follow-up question about skipping the first line, the simplest way is to use itertools.islice : 至于有关跳过第一行的后续问题,最简单的方法是使用itertools.islice

from itertools import islice    
for line in islice(open(full_path), 1, None):
    fout.write(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM