0

All the tutorials I can find follow the same format which isn't working.I don't get an error message but I don't get normal output. What I get appears to be the file description at some memory location.

# file_test

ftpr= open("file","w")
ftpr.write("This is a sample line/n")
a=open("file","r")
print a




#This is the result

<open file 'file', mode 'r' at 0x00000000029DDDB0>
>>> 

2 Answers 2

2

Do you want to read the contents of the file? Try print a.readlines().

Ie:

with open('file', 'w') as f:
  f.write("Hello, world!\nGoodbye, world!\n")

with open('file', 'r') as f:
  print f.readlines()  # ["Hello, world!\n", "Goodbye, world!\n"]

FYI, the with blocks, if you're unfamiliar with them, ensure that the open()-d files are close()-d.

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

Comments

0

This is not the correct way to read the file. You are printing return value from open call which is object of file type. Do like this for reading and writing.

for writing

f=open("myfile","w")

f.write("hello\n")

f.write("This is a sample line/n")

f.close()

For reading

f=open("file","r")

string = f.read()

print("string")

f.close()

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.