Re: noob: print and file.read()
On 2012-12-03 23:06, Andrew Z wrote:
> why the following code doesn't print the content of the file:
> from_file ="file.txt"
> in_file = open(from_file)
> str = in_file.read()
You've read the entire file, leaving the file pointer positioned at the
end of the file.
> print "Here should be the output from the file - ", in_file.read()
You're trying to read the file again, but the file pointer is still
positioned at the end of the file, and there's no more left to read.
If you want to read it again, you'll need to reset the file pointer
back to the start of the file using in_file.seek(0).
> print "Here should be the output from the STR- ", str
> The first "print" has nothing whereas the second properly displays the
> content of the file.
|All times are GMT. The time now is 01:40 AM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.