Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't read any character in this Python code

Tags:

python

file

io

I know, w+ means both read and write, then I write this code to read some message from a txt file and write some message into it

This is my code:

f = open('test', 'w+')
f.write('yes yes yes\n')
print f.read()
......
f.close()

when i run it, It only print newline, and didn't print yes yes yes

then I open test file, I found yes yes yeswas in it!

Why it' just print new line, but print yes, and what's the difference between w+ & r+

like image 711
thlgood Avatar asked Dec 12 '25 01:12

thlgood


1 Answers

You should put cursor at the beginning of file before reading.

f.seek(0)
f.read()
like image 116
kev Avatar answered Dec 14 '25 22:12

kev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!