繁体   English   中英

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

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

循环读取目录中的所有文本文件,并从每个文本文件的第一行打印值(按预期方式),但仅向该文件写入一行。

我已经尝试过使用for和while循环的几种循环形式,但是我认为我做错了。 我之所以被淘汰是因为它可以打印正确的输出(几行),但是只写一行。

# 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()

预期的结果是usage.txt显示与控制台中打印的输出相同的输出。

usage.txt只有一行: 30_33

控制台将打印所有行:

10_36

10_36

20_58

20_0

20_58

30_33

30_33

流程结束,退出代码为0

您正在打开和截断文件,使用append打开:

既然你打开每次循环的文件,并没有使用a ,你是截断文件中的每个循环,使每次循环你写一个新的1号线的文件。

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

每次在循环中打开文件时,都会重新创建文件。 您应该在循环之前将其打开一次。

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