ruthless
ruthless

Reputation: 309

How to open a text file in python?

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.

Upvotes: 0

Views: 259

Answers (1)

chanakya devraj
chanakya devraj

Reputation: 148

f = open("file.txt",'r+')
lines = f.readlines()
f.writelines(lines)
f.close()

Upvotes: 1

Related Questions