简体   繁体   English

如何将多个文本文件合并为一个文件?

[英]How to combine several text files into one file?

I want to combine several text files into one output files. 我想将几个文本文件合并为一个输出文件。 my original code is to download 100 text files then each time I filter the text from several words and the write it to the output file. 我的原始代码是下载100个文本文件,然后每次我从几个单词中过滤文本并将其写入输出文件时。

Here is part of my code that suppose to combine the new text with the output text. 这是我的代码的一部分,该代码假定将新文本与输出文本组合在一起。 The result each time overwrite the output file, delete the previous content and add the new text. 每次结果都会覆盖输出文件,删除先前的内容并添加新的文本。

import fileinput
import glob    
urls = ['f1.txt', 'f2.txt','f3.txt']

N =0;
print "read files"
for url in urls:
 read_files = glob.glob(urls[N])
 with open("result.txt", "wb") as outfile:
    for f in read_files:
        with open(f, "rb") as infile:
            outfile.write(infile.read())
 N+=1

and I tried this also 我也尝试过

import fileinput
import glob
urls = ['f1.txt', 'f2.txt','f3.txt']

N =0;
print "read files"
for url in urls:
 file_list = glob.glob(urls[N])
 with open('result-1.txt', 'w') as file:
    input_lines = fileinput.input(file_list)
    file.writelines(input_lines)
 N+=1

Is there any suggestions? 有什么建议吗? I need to concatenate/combine approximately 100 text files into one .txt file In sequence manner. 我需要按顺序将大约100个文本文件串联/组合为一个.txt文件。 (Each time I read one file and add it to the result.txt) (每次我读取一个文件并将其添加到result.txt中)

The problem is that you are re-opening the output file on each loop iteration which will cause it to overwrite -- unless you explicitly open it in append mode. 问题是您将在每次循环迭代时重新打开输出文件,这将导致其覆盖-除非您在追加模式下显式打开它。

The glob logic is also unnecessary when you already know the filename. 当您已经知道文件名时,也不需要使用全局逻辑。

Try this instead: 尝试以下方法:

with open("result.txt", "wb") as outfile:
    for url in urls:
        with open(url, "rb") as infile:
            outfile.write(infile.read())

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

相关问题 将多个文本文件合并为一个文本文件? - combine multiple text files into one text file? 将列添加到几个文件,合并到一个文件中并对其进行排序 - Add column to several files, combine in one file and sort it python将两个文本文件合并到一个文件中 - python combine two text files to one file 如何在文本文件中追加数据并使用 python 将多个文本文件合并为一个文本文件? - How do I append the data in text files and combine multiple text files into one text file using python? 如何将普通的 function 与另一个 function 结合起来,将不同的文本文件合并到一个文件中? - How to combine a normal function with another function that will combine different text files into one file? 如何将几个类似的.csv文件组合成一个给定结构的数据帧 - How to combine several similar .csv files into one dataframe with given structure 将多个 xlsx 文件合并为一个 xlsx - Combine several xlsx files into one single xlsx 如何将两个文本文件合并为一个? - How to combine two text file as one? 如果多个大文本文件太大而无法单独转换,如何将它们转换为一个 CSV 文件? - How do I convert several large text files into one CSV file if they are too large to be converted individually? 使用 python 将多个未排序的文本文件合并为一个排序文件 - Combine multiple unsorted text files into one sorted file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM