After one type() in the middle, the value does not appear in the last print (diary).
If you try f.seek(0), you get the same result.
I wonder why this is happening.
Thank you.
python
The read()
method in the file reads all the text in the file and returns it as a string if nothing is specified by the argument.
In type(f.read()
, f.read()
inside got all the text as a string and put it in
type()
so <class 'str'>
was output.
Since then, diary=f.read()
has been re-run, but I can't read any more because I've already read all the text in the file before. Therefore, f.read()
returns an empty string '
and
print(diary)
outputs an empty string, which outputs one white blank line on the screen.
And f.If you use see(0)
, you must use it before f.read()
again, as shown below.
f=open('a.txt','r')
type(f.read())
f.seek(0)
diary=f.read()
print(diary)
-Result
919 Uncaught (inpromise) Error on Electron: An object could not be cloned
1343 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
835 Error in x, y, and format string must not be None
801 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2025 OneMinuteCode. All rights reserved.