简体   繁体   English

如何读取文件夹中的多个.txt 文件并使用 python 写入单个文件?

[英]How to read multiple .txt files in a folder and write into a single file using python?

I am trying to read files with.txt extension and wanted to append into a single txt file.我正在尝试读取扩展名为 .txt 的文件,并希望将 append 转换为单个 txt 文件。 I could read data.我可以读取数据。 But what is the best way to write into single.txt file?但是写入 single.txt 文件的最佳方法是什么?

Try this.尝试这个。

x.txt: x.txt:

Python is fun

y.txt: y.txt:

Hello World. Welcome to my code.

z.txt: z.txt:

I know that python is popular.

Main Python file:主要 Python 文件:

list_=['x.txt','y.txt','z.txt']
new_list_=[]
for i in list_:
    x=open(i,"r")
    re=x.read()
    new_list_.append(re)
with open('all.txt',"w") as file:
    for line in new_list_:
        file.write(line+"\n")
sources = ["list of paths to files you want to write from"]
dest = open("file.txt", "a")
for src in sources:
    source = open(src, "r")
    data = source.readlines()
    for d in data:
        dest.write(d)
    source.close()
dest.close()

If your destination doesnt already exist you can use "w"(write) mode instead of "a"(append) mode.如果您的目的地不存在,您可以使用“w”(写入)模式而不是“a”(附加)模式。

After you find the filenames, if you have a lot of files you should avoid string concatenation when merging file contents because in python string concatenation comes with O(n) runtime cost.找到文件名后,如果您有很多文件,则在合并文件内容时应避免字符串连接,因为在 python 中,字符串连接会带来 O(n) 运行时成本。 I think the code below demonstrates the full example.我认为下面的代码演示了完整的示例。

import glob

# get every txt files from the current directory
txt_files = glob.iglob('./*.txt')

def get_file_content(filename):
    content = ''
    with open(filename, 'r') as f:
        content = f.read()
    return content

contents = []
for txt_file in txt_files:
    contents.append(get_file_content(txt_file))

with open('complete_content.txt', 'w') as f:
    f.write(''.join(contents))

暂无
暂无

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

相关问题 使用 python,如何读取文件夹中的所有 CSV 文件并将其内容写入新的单个 CSV 文件? - Using python, how to read all the CSV files in a folder and write the content of the same to a new single CSV file? 在单个文件夹中读取多个.txt文件 - Read multiple .txt files in a single folder 如何从需要在单独文件夹中读写 .txt 文件的 .py 文件创建 python 可执行文件 - How to create python executable from a .py file that need to read and write .txt files in a separate folder 如何使用 Python 将文件夹中的 all.txt 文件和 append 其内容读取到 one.txt 文件中? - How to read all .txt files in a folder and append its contents into one .txt file, using Python? 如何通过Python将单个txt文件拆分为多个txt文件 - how to split single txt file into multiple txt files by Python 每次使用pandas和python处理多个文件时,从txt文件中读取单个变量 - Read single varible from txt file each time processing multiple files using pandas and python 如何依次读取两个txt文件并将其写入python中的新文件? - how to sequentially read two txt files and write into a new file in python? python将txt文件读入单个文件 - python read txt files into single file 使用python将多个txt文件中的内容转换为单个excel文件 - content from multiple txt files into single excel file using python 有没有办法使用 Python 读取和写入文件夹中的多个文本文件? - Is there a way to read and write on multiple text files in a folder using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM