未正确覆盖文本文件 (Python)

Not overwriting text file properly (Python)

我的程序应该获取一个文本文件,打印内容,计算行数,要求输入一个字符串,删除该字符串的每次出现,说明该字符串被删除了多少次,然后输出新的文件的内容。我们被要求使用一个文件,而不是输入和输出文件。

我的代码可以正常工作并执行所有它应该做的事情,直到我尝试将更改存储在 char_count 函数末尾的文件中并且 print_output 函数似乎无法正常工作完全没有。

如果我有一个输入文件的内容:

Apples
Oranges
Bananas
Apples
Oranges
Bananas

如果我尝试删除 Bananas,输入文件的结果文件内容为:

ApplesOrangesApplesOrangesles
Oranges
Bananas

我一直想弄明白是怎么回事,但没有任何进展,而且我们的课程教科书似乎没有提到覆盖输入文件,但我们需要为作业做这件事。我的最后两个功能有什么问题?

def main():

    input_file_name = input("Please Enter the name of your text file: ")
    infile = open(input_file_name, "r+")
    print()

    print("---------------------------------------")
    print("THE FILE CONTENTS ARE")
    print("---------------------------------------")
    print_file(infile)
    print("---------------------------------------")
    count_lines(infile)
    print("---------------------------------------")
    input_string = input("Please enter the word or string of words you want to remove from the text file: ")
    print("---------------------------------------")
    char_count(infile, input_string)
    print("---------------------------------------")
    print("THE NEW FILE CONTENTS ARE")
    print_output(infile)
    print("---------------------------------------")

    infile.close()

def print_file(infile):
    infile.seek(0)
    allLines = infile.readlines()
    for line in allLines:
        text = line.rstrip()
        print(text)

def count_lines(infile):
    infile.seek(0)
    allLines = infile.readlines()
    count = 0
    char = " "
    for line in allLines :
        text = line.rstrip()
        while char != "":
            char = infile.read(1)
        count = count + 1
    print("THE NUMBER OF LINES IS: %d " % count)

def char_count(infile, input_string) :
    count = 0
    infile.seek(0)
    allLines = infile.readlines()
    infile.seek(0)
    for line in allLines:
        while input_string in line:
            line = line.replace(input_string, "")
            count = count + 1
        text = line.rstrip()
        infile.write(text)
    print("NUMBER OF OCCURRENCES REMOVED IS: %d" % count)

def print_output(infile):
    infile.seek(0)
    allLines = infile.readlines()
    for line in allLines:
        text = line.rstrip()
        print(text)
main()

您必须先截断文件以获得所需的输出。

def main():

    input_file_name = input("Please Enter the name of your text file: ")
    infile = open(input_file_name, "r+")
    print()

    print("---------------------------------------")
    print("THE FILE CONTENTS ARE")
    print("---------------------------------------")
    print_file(infile)
    print("---------------------------------------")
    count_lines(infile)
    print("---------------------------------------")
    input_string = input("Please enter the word or string of words you want to remove from the text file: ")
    print("---------------------------------------")
    char_count(infile, input_string)
    print("---------------------------------------")
    print("THE NEW FILE CONTENTS ARE")
    print_output(infile)
    print("---------------------------------------")

    infile.close()

def print_file(infile):
    infile.seek(0)
    allLines = infile.readlines()
    for line in allLines:
        text = line.rstrip()
        print(text)

def count_lines(infile):
    infile.seek(0)
    allLines = infile.readlines()
    count = 0
    char = " "
    for line in allLines :
        text = line.rstrip()
        while char != "":
            char = infile.read(1)
        count = count + 1
    print("THE NUMBER OF LINES IS: %d " % count)

def char_count(infile, input_string) :
    count = 0
    infile.seek(0)
    allLines = infile.readlines()
    infile.seek(0)
    infile.truncate() #Empty your file first to rewrite it
    for line in allLines:
        while input_string in line:
            line = line.replace(input_string, "")
            count = count + 1
        text = line.rstrip() 
        if(text != ""):
            infile.write(text + "\n") #To write in multiple lines
    print("NUMBER OF OCCURRENCES REMOVED IS: %d" % count)

def print_output(infile):
    infile.seek(0)
    allLines = infile.readlines()
    for line in allLines:
        text = line.rstrip()
        print(text)
main()