简体   繁体   中英

Python won't read text file

I've tried looking online for a way to fix this but nothing works.

Python is supposed to read a file called "directory.txt" but it keeps coming up either blank or says:

<_io.TextIOWrapper name='directory.txt' mode='r' encoding='cp1252'>

The idea is that it's supposed to let the user add names and emails to the txt files allowing them to either "read" the file or "add" to it.

Code:

command = input("What would you like to do? Read or add? >> ")
programactive = True

if command == "Read" or "read":

    directory = open('directory.txt', 'r')
    directory.read()
    print(directory)
    directory.close()


elif command == "Add" or "add":

    while programactive == True:
        directory = open('directory.txt', 'a')
        new_name = input("Add a new name to the list. >> ")
        new_email = input("Add a new email for that name. >> ")
        combined = new_name + ", " + new_email
        directory.write(combined)
        cont = input("Add more? Yes or No >> ")
        if cont == "No" or "no":
            directory.close()
            programactive = False

I've written the code here, it is working:

import io

programactive = True

command = input("What would you like to do? Read or add? >> ")

if command == "Read" or command == "read":
   directory = open("directory.txt", 'r')
   read_string = directory.read()
   print(read_string)
   directory.close()
elif command == "Add" or command == "add":
   while programactive == True:
      directory = open('directory.txt', 'a')
      new_name = input("Add a new name to the list. >> ")
      new_email = input("Add a new email for that name. >> ")
      combined = new_name + ", " + new_email + ", "
      directory.write(combined)
      cont = input("Add more? Yes or No >> ")
      if cont == "No" or "no":
         directory.close()
         programactive = False
else:
   print("Invalid command...")

A few issues I see in this code:

1) You're printing the pointer to the memory location of the directory object, rather than its contents.
2) Your conditional logic for Read and Add and No are not properly checking both conditions. 3) You aren't appending a newline character to each added entry, so text is going to show up on one line, not delimited.

For #1, you just need to store the contents of directory.read() in a string variable and then print the string, instead of printing the object itself. For #2, when you have multiple equality conditions, you have to explicitly define both sides of the equality (eg if command == "Read" or command == "read": instead of just if command == "Read" or "read:" And for #3, you should just need to add a \\n to your "combined" variable.

Try out the following code, and test the "Add" functionality then check the file to make sure that the added text comes through formatted as you expect:

command = input("What would you like to do? Read or add? >> ")
programactive = True

if command == "Read" or command == "read":

    directory = open('directory.txt', 'r')
    contents = directory.read()
    print(contents)
    directory.close()


elif command == "Add" or  command == "add":

    while programactive == True:
        directory = open('directory.txt', 'a')
        new_name = input("Add a new name to the list. >> ")
        new_email = input("Add a new email for that name. >> ")
        combined = "\n" + new_name + ", " + new_email
        directory.write(combined)
        cont = input("Add more? Yes or No >> ")
        if cont == "No" or cont == "no":
            directory.close()
            programactive = False 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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