0

I have the following code:

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

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

2 Answers 2

2

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:
    print(file_obj.readlines())

Take a closer look at the docs next time.

Sign up to request clarification or add additional context in comments.

1 Comment

You can read any file the same way. The difference would be in how will you treat the contents.
0

You can also try the following :

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

It prints out all the lines in the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.