Kathir Kathir - 1 year ago 155
Python Question

Reading a text file using python

I have the following code:

readFromFile = open('C:\\Users\\sunka\\Desktop\\exampleFile.txt','r')

After running the code, I am getting the following issue

<_io.TextIOWrapper name='C:\\Users\\sunka\\Desktop\\exampleFile.txt' mode='r' encoding='cp1252'>

it's not printing the contents in the file.

Kindly help me to fix this

Answer Source

Your readFromFile variable is a file object, that you can read data from. The readlines function returns array of lines inside that opened file.

So what you want to do is:

with open('C:\Users\sunka\Desktop\exampleFile.txt','r') as file_obj:

Take a closer look at the docs next time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download