简体   繁体   English

将所有txt文件的内容合并为一个- Python

[英]consolidated contents of all txt files into one- Python

I'm trying to consolidate all the result.txt file contents inside result folder and generating "Result_STD.txt" file out of it.我正在尝试合并结果文件夹中的所有 result.txt 文件内容并从中生成“Result_STD.txt”文件。 But before writing to a file I want to add a str(GenerateFile) once but currently for every file append that string is getting added.但在写入文件之前,我想添加一个 str(GenerateFile) 一次,但目前为每个文件 append 添加该字符串。 Can anyone please suggest me how to add that string only once in a file?谁能建议我如何在文件中只添加一次该字符串?

def createOutput(self,STD):
    directory = "/result"
    read_files = glob.glob(directory + "/" + "*_result.txt")
    with open(directory + "/" + "Result_" + STD + ".txt", "wb") as outfile:
        for f in read_files:
            with open(f, "rb") as infile:
                data = infile.read()
                new_data = '{"GenerateFile":'+ data +  '}'
                print(new_data)
=================================================================
                
Current Output:

GenerateFile:{
    "Message": "CREATED", 
    "Result": "DONE"
}
GenerateFile:{
    "Message": "CREATED", 
    "Result": "DONE"
}

==============================================================
                
expected output:

GenerateFile:{
    "Message": "CREATED", 
    "Result": "DONE"
},
{
    "Message": "CREATED", 
    "Result": "DONE"
}

Does this do what you need it to do?这能满足您的需要吗?

If so, all I did was move the new_data string initial assignment of GenerateFile: out of the for f in read_files: loop and append to it inside the loop, then print the output at the end of the loop.如果是这样,我所做的就是将GenerateFile:new_data字符串初始分配移出for f in read_files:循环和 append 到循环内,然后在循环结束时打印 output 。

def createOutput(self,STD):
    directory = "/result"
    read_files = glob.glob(directory + "/" + "*_result.txt")
    with open(directory + "/" + "Result_" + STD + ".txt", "wb") as outfile:
        new_data = "GenerateFile:"
        for f in read_files:
            with open(f, "rb") as infile:
                data = infile.read()
                new_data = new_data + " {" + data + "}, "
        print(new_data)

You might need to remove the last , before print(new_data) if it's a problem.如果有问题,您可能需要在print(new_data)之前删除最后一个,

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

相关问题 如何使用 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? - Python: Convert all files in directory into one .TXT? 比较Python中txt文件的内容 - Comparing contents of txt files in Python 用新的 Python tkinter 替换窗口 - Replacing a window with a new one- Python tkinter Python:我如何将output目录下的所有文本内容包含.txt文件作为一个列表? - Python: How do I output all the text contents of a directory containing .txt files as a list? 取一个文件的列平均值,对所有.txt文件重复,将所有平均值写入python中的一个文件 - Take column average of one file, repeat for all .txt files, write all averages to one file in python 比较两个 txt.file 的内容以查找已删除的行或 python 中的更改 - Comparing contents of two txt.files for deleted lines or changes in python 在 Python 中将所有具有不同扩展名的文件重命名为 .txt - Rename all files with different extensions to .txt in Python 尝试在Python中访问所有.txt文件 - Trying to reach all .txt files in Python 复制并粘贴多个txt文件的内容以创建一个大文件 - copy and paste the contents of many txt files to create one big file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM