简体   繁体   English

如何在python中添加和合并txt文件?

[英]How to prepend and merge txt files in python?

I am trying to merge and prepend a new line to each txt file using Python.我正在尝试使用 Python 合并并在每个 txt 文件前添加一个新行。 So the goal is to merge all 200 text files into one text file and a new line TEXT, to all 200 files as the first line of data for the text file.所以目标是将所有200个文本文件合并成一个文本文件和一个新行TEXT,将所有200个文件作为文本文件的第一行数据。 Should I prepend and then merge to make life easier?我应该预先添加然后合并以使生活更轻松吗?

I tried merging and prepending a new line to the existing txt file but when I open the result file, it deltes all the content in the file and adds a new line TEXT, .我尝试将新行合并并添加到现有的 txt 文件中,但是当我打开结果文件时,它删除了文件中的所有内容并添加了一个新行TEXT, . Anyway I can fix this?无论如何我可以解决这个问题吗?

import glob

read_files = glob.glob("*.txt")

with open("result.txt", "wb") as outfile:
    for f in read_files:

        with open(f, "r") as infile:
            outfile.write(infile.read())

            with open(f,"w+") as infile:
                infile.write("TEXT,"+infile.read())
                with open(f,"r+") as infile:
                    b=infile.read()
                    print b

Actual Result:
TEXT,

Desired Result TEXT, ……………(JUST RANDOM DATA)

This should do what you want...or be pretty darn close...这应该做你想做的......或者非常接近......

import fileinput
import glob

# Creating a list of filenames 
filenames = glob.glob("C:\\your_path_here\\*.txt")

# Open file3 in write mode 
with open('consolidated.txt', 'a') as outfile: 

    # Iterate through list 
    for names in filenames: 

        # Open each file in read mode 
        with open(names) as infile: 

            # read the data from file
            outfile.write(infile.read()) 
            outfile.write('\n\n' + 'TEXT' + '\n\n')

Final Results in Text File:文本文件中的最终结果:

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

FName,LName,Address
Jim,Bentz,34 Holloway La.
George,Hororitz,76 Ridge Dr.
Eric,Schimtz,11 Main St.

TEXT

There are a few things to be aware of with these kinds of things.有一些事情需要注意这些事情。

The first step in writing to a file is create the file object by using the built-in Python command "open".写入文件的第一步是使用内置的 Python 命令“open”创建文件对象。

To create and write to a new file, use open with "w" option.要创建和写入新文件,请使用 open with "w" 选项。 The "w" option will delete any previous existing data in the file and write (new contents) to the file. “w”选项将删除文件中任何先前存在的数据并将(新内容)写入文件。 The "a" option will append to a file, rather than write (and overwrite) to a file. “a”选项将附加到文件,而不是写入(和覆盖)到文件。 The "r" option is used to read a file. “r”选项用于读取文件。 See the link below for more info.有关更多信息,请参阅下面的链接。

https://www.tutorialsteacher.com/python/python-read-write-file https://www.tutorialsteacher.com/python/python-read-write-file

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

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