没有正确覆盖文本文件(Python)

时间:2017-11-13 07:51:01

标签: python function readlines

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

我的代码工作并完成它应该做的所有事情,直到我尝试将更改存储在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()

1 个答案:

答案 0 :(得分:1)

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

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