Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix Python io.TextIOWrapper error message?

Tags:

python

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'>
like image 375
Part_Time_Nerd Avatar asked Mar 26 '26 17:03

Part_Time_Nerd


1 Answers

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()
like image 146
Part_Time_Nerd Avatar answered Mar 28 '26 07:03

Part_Time_Nerd