简体   繁体   English

如何在python 3中将列表写入txt文件?

[英]how to write a list into a txt file in python 3?

I want to make a program (Python 3.6) that can read multiple log files and write all the lines from those files into a txt file. 我想制作一个程序(Python 3.6),它可以读取多个日志文件并将这些文件中的所有行写入txt文件。

The code that I've already tried can read all the lines, but can't write all lines to a txt file. 我已经尝试过的代码可以读取所有行,但是不能将所有行都写入txt文件。 I've tried this: 我已经试过了:

allFiles = glob.glob('C:\Program Files\PostgreSQL\9.6\data\pg_log\*.log')
def readFile(allFiles):
    for file in allFiles:
        f = open(file,'r')
        allLines = []
        for line in f:
            allLines.append(line)
            print(line)
        f.close()

    with open ('readFile.txt',mode='wt', encoding='utf-8') as fileOutput:
        for line in allLines:
            fileOutput.write(line)
        fileoutput.close()

I expect all the lines from all files can written in a txt file, but results I've got was lines written in txt only line that have the same date as the date of execution of this program. 我希望所有文件中的所有行都可以写入txt文件中,但是我得到的结果是仅以txt编写的行与该程序的执行日期具有相同的日期。

What should I do ? 我该怎么办 ?

You are resetting allLines to an empty list each time you read a file. 每次读取文件时,会将allLines重置为空列表。 Move the allLines = [] line to outside your for loop. allLines = []行移到您的for循环之外。 In other words, the start of your function should be: 换句话说,函数的开始应为:

def readFile(allFiles):
    allLines = []
    for file in allFiles:
        f = open(file,'r')

Also, a couple "style notes": It is generally considered bad form to use a variable name that already has meaning in Python, even though the language allows it. 另外,还有一些“样式注释”:使用已在Python中具有含义的变量名称,即使该语言允许,通常也被认为是不好的形式。 Thus it would be a good idea to use something other than "file". 因此,最好使用“文件”以外的东西。 Also, Python convention is that underscores are used between words in variable names, not Camel Case. 同样,Python约定是在变量名中的单词之间使用下划线,而不是Camel Case。 Thus "allLines" would be "all_lines". 因此,“ allLines”将是“ all_lines”。 You might want to see the Python Style Guide at: https://www.python.org/dev/peps/pep-0008/ 您可能希望在以下位置查看Python样式指南: https//www.python.org/dev/peps/pep-0008/

alllines only holds the lines from the last read file - because you reset it inside the loop to alllines=[] . alllines仅保存最后读取的文件中的行-因为您在循环将其重置为alllines=[] You need to move this before the loop (or get rid of it - see below). 您需要在循环之前将其移动(或摆脱它-参见下文)。

It would be far easiert to simply concat the files: see Python concatenate text files 简单地合并文件会容易得多:请参阅Python合并文本文件


Your code has a second/third problem - if you have 20 files that each have 1 GB you store 20GB ... in memory ... which is highly inefficient. 您的代码有第二个/第三个问题-如果您有20个文件,每个文件具有1 GB,则将20GB ...存储在内存中...效率很低。

It would be better to just write line by line into your new file. 最好只逐行写入新文件。 Beside that your should use the with open(...) as ..: paradigma when using file objcts like so: 此外,在使用文件objcts时,应使用with open(...) as ..: paradigma:

def readFile(allFiles):
    with open ('readFile.txt', mode='wt', encoding='utf-8') as fileOutput:
        for file in allFiles:
            with open(file) as reader:
                for line in reader:
                    fileOutput.write(line)


allFiles = glob.glob('C:\Program Files\PostgreSQL\9.6\data\pg_log\*.log')
readFile(allFiles)

See Python 3.7 - File reading/writing 参见Python 3.7-文件读取/写入

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

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