简体   繁体   English

循环打印多行(按预期),但只向文件写入一行

[英]Loop prints multiple lines (as intended) but only writes one line to the file

The loop reads all text files in the directory and prints the value from the first line of each text file (as expected) but only writes one line to the file. 循环读取目录中的所有文本文件,并从每个文本文件的第一行打印值(按预期方式),但仅向该文件写入一行。

I have tried several variants of the loop using for and while loops but I think I may be doing them wrong. 我已经尝试过使用for和while循环的几种循环形式,但是我认为我做错了。 I am thrown off because it prints the correct output (several lines) but only writes one line. 我之所以被淘汰是因为它可以打印正确的输出(几行),但是只写一行。

# this program reads the files in this directory and gets the value in the first line of each text file
# it groups them by the first two numbers of their filename

import glob

# get list of all text files in directory
path = "./*.txt"
txt_files = glob.glob(path)

# these are the groups I need to sort the results by
a1 = "10"
a2 = "20"
b1 = "30"
c1 = "40"

# list of files is in txt_files

for fileName in txt_files:

    # get the two digits from the filename to group the files
    device = fileName[2:4]

    # if the file name's first two digits (device) match the variable, open the file and get the value in the first line

    if device == a1:
        file = open(fileName)
        line = file.readline()
        # then, write that first line's value to the usage.txt file
        print(device + "_" + line)
        fileU = open("usage.txt", 'w')
        fileU.write(device + "_" + line + "\n")
        file.close()

    # if the file name's first two digits = 20, proceed

    elif device == a2:
        # open the text file and get the value of the first line
        file = open(fileName)
        line = file.readline()
        print(device + "_" + line)
        fileU = open("usage.txt", 'w')
        fileU.write(device + "_" + line + "\n")
        file.close()

    # if the file name's first two digits = 30, proceed

    elif device == b1:
        file = open(fileName)
        line = file.readline()
        print(device + "_" + line)
        fileU = open("usage.txt", 'w')
        fileU.write(device + "_" + line + "\n")
        file.close()

The expected results would be usage.txt showing the same output as what is printed in the console. 预期的结果是usage.txt显示与控制台中打印的输出相同的输出。

usage.txt will only have one line: 30_33 usage.txt只有一行: 30_33

the console will print all of the lines: 控制台将打印所有行:

10_36 10_36

10_36 10_36

20_58 20_58

20_0 20_0

20_58 20_58

30_33 30_33

30_33 30_33

Process finished with exit code 0 流程结束,退出代码为0

You are opening and truncating the file, open with append : 您正在打开和截断文件,使用append打开:

Since you open the file each time you loop, and you are not using a , you are truncating the file each loop so each loop you write a new 1 line file. 既然你打开每次循环的文件,并没有使用a ,你是截断文件中的每个循环,使每次循环你写一个新的1号线的文件。

fileU = open("usage.txt", 'a')

You're recreating the file each time you open it in the loop. 每次在循环中打开文件时,都会重新创建文件。 You should open it once before the loop. 您应该在循环之前将其打开一次。

with open("usage.txt", "w") as fileU:
    for fileName in txt_files:

        # get the two digits from the filename to group the files
        device = fileName[2:4]

        # if the file name's first two digits (device) match the variable, open the file and get the value in the first line

        if device == a1:
            file = open(fileName)
            line = file.readline()
            # then, write that first line's value to the usage.txt file
            print(device + "_" + line)
            fileU.write(device + "_" + line + "\n")
            file.close()

        # if the file name's first two digits = 20, proceed

        elif device == a2:
            # open the text file and get the value of the first line
            file = open(fileName)
            line = file.readline()
            print(device + "_" + line)
            fileU.write(device + "_" + line + "\n")
            file.close()

        # if the file name's first two digits = 30, proceed

        elif device == b1:
            file = open(fileName)
            line = file.readline()
            print(device + "_" + line)
            fileU.write(device + "_" + line + "\n")
            file.close()

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

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