0

I try to do the following,

file = open('test.txt', 'w+')
item = "hello"

file.write(item)
print(file)

When I run this program I get the following output,

<_io.TextIOWrapper name='test.txt' mode='w+' encoding='cp1252'>

Is there a way to open and then write in the file and then save it so I can use that new file somewhere else? Even though I have something written in the file, I still get this output.

3
  • possible duplicate of Correct way to write line to file in Python Commented Dec 14, 2013 at 14:00
  • What do you expect print(file) to do? Commented Dec 14, 2013 at 14:05
  • @tim thanks i just go confused. i now understand what i did wrong. Commented Dec 14, 2013 at 20:30

1 Answer 1

1
f = open("file.txt",'r+')
lines = f.readlines()
f.writelines(lines)
f.close()
Sign up to request clarification or add additional context in comments.

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.