Home > front end >  How to open and edit a txt file in a while loop?
How to open and edit a txt file in a while loop?

Time:11-07

First, I'm very new to python and this college class I took has been very unhelpful so sorry in advance.

I have a text document with book names. I need to make the user input an option. Each option coinsides with either displqaying the text document, editing the text document, or just stopping the program. I have the last one. I can display it out of a loop. But, if I try to do it in a loop It jsut doesn't do anything, no error, just nothing.

f = open("Books.txt")
for line in f:
    print (line)

inp = ()
while inp != 3:
    print("1 - Display, 2 - Add, 3 - Exit")
    inp = input("Please select an option.")
    inp = int(inp)
if inp == 1:
    print (f)

CodePudding user response:

You can do this by using tell() and seek() methods with file object (f)

Here is the code for it

f = open("Books.txt", "a ")
inp = 0

while inp != 3:
    print("1 - Display, 2 - Add, 3 - Exit")
    inp = input("Please select an option.")
    inp = int(inp)
    if inp == 1:
        if f.tell() != 0:
            f.seek(0)
        for line in f:
            if line.strip():
                print (line.strip())
    elif inp == 2:
        book = input("What is your book name? ")
        f.writelines(["\n" book])
   
f.close()

The output of this is as follows

1 - Display, 2 - Add, 3 - Exit
Please select an option.1
HELLO
WORLD
TEST
1 - Display, 2 - Add, 3 - Exit
Please select an option.2
What is your book name? Test2
1 - Display, 2 - Add, 3 - Exit
Please select an option.1
HELLO
WORLD
TEST
Test2
1 - Display, 2 - Add, 3 - Exit
Please select an option.3

Code explanation as follows

  1. Open the file with append mode with readable access (https://docs.python.org/3/library/functions.html#open)

  2. The file cursor will be at EOF (End of File) in this case so you can easily append the new text to the file

  3. When the input is 1 we first check the cursor location using tell() which returns the integer value of the number of bytes read and if it's not in the starting. We move the cursor at starting of the file using seek(0) (https://docs.python.org/3/tutorial/inputoutput.html)

  4. Since there are could be empty lines containing only new line characters which we can filter out while printing and later print the text by again stripping off the white characters using the strip() method on the string because the print function automatically adds new line character

  5. To write the line we need to pass a new character (line separator of course) and then the input name of the book

CodePudding user response:

f = open("Books.txt", "r ")
inp = 0

while inp != 3:
    print("1 - Display, 2 - Add, 3 - Exit")
    inp = input("Please select an option.")
    inp = int(inp)
    if inp == 1:
        for line in f:
            print (line)
    elif inp == 2:
        book = input("What is your book name? ")
        f.writelines(["\n" book])
   
f.close()
exit()
  • Related