Part_Time_Nerd
Part_Time_Nerd

Reputation: 1014

How do I fix Python io.TextIOWrapper error message?

I know there are several post that have asked this question and I have tried some of the solutions but I still keep getting the error message. Both of the following solutions produce the same error message. What am I doing wrong?

Here is one solution I tried:

def main():
    #Open a file named numbers.txt
    numbers_file = open('numbers.txt','r')

    #read the numbers on the file
    file_contents = numbers_file.read()

    #Close the the numbers file
    numbers_file.close()

    #Print the data that was inside the file
    print(numbers_file)

#Call the main function
main()

Here is another solution I tried:

with open(r"numbers.txt",'r') as numbers_file:
    #read the numbers on the file
    file_contents = numbers_file.read()

    #Close the the numbers file
    numbers_file.close()

    #Print the data that was inside the file
    print(numbers_file)

The error message I get when I run either of the programs is:

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

Upvotes: 3

Views: 27856

Answers (2)

Yuri Melnik
Yuri Melnik

Reputation: 11

Thanks for answers.

This isn't the error message.

The code print(numbers_file) tells you about numbers_file object.

Really, you have to print(file_contents).

Upvotes: 1

Part_Time_Nerd
Part_Time_Nerd

Reputation: 1014

Thanks for the help. I realized what I was doing wrong. Here is what I got:

def main():
    #Open a file named numbers.txt
    numbers_file = open('numbers.txt','r')

    #read the numbers on the file
    file_contents = numbers_file.read()

    #Close the the numbers file
    numbers_file.close()

    #Print the data that was inside the file
    print(file_contents)

#Call the main function
main()

Upvotes: 3

Related Questions