[Tutor] Questions on file.read

Kent Johnson kent37 at tds.net
Thu Jul 14 15:17:36 CEST 2005


Negroup - wrote:
> read(...)
>     read([size]) -> read at most size bytes, returned as a string.
> 
>     If the size argument is negative or omitted, read until EOF is reached.
>     Notice that when in non-blocking mode, less data than what was requested
>     may be returned, even if no size parameter was given.
> 
> Hi tutors.
> What does "blocking-mode" mean, and how can I be sure that when
> reading a file I'm not in such modality (and eventually switch)? I
> need to read the whole content of a file, not just some pieces.

I don't know how to put a file in non-blocking mode but you don't have to worry about it for common usage. Use f.read() (with no arguments) to read the whole contents of a file.

> 
> Another question. Python tutorial states:
> 
> "To read a file's contents, call f.read(size), which reads some
> quantity of data and returns it as a string. size is an optional
> numeric argument. When size is omitted or negative, the entire
> contents of the file will be read and returned; it's your problem if
> the file is twice as large as your machine's memory."
> 
> What does exactly mean that it's my problem (crash? an exception will
> be raised? fire and flames? xyz?). How can my recognize a "too big
> file" before read it?

My guess is that it will raise MemoryError but I don't know for sure. Use os.path.getsize() to find out how big a file is.

Kent


More information about the Tutor mailing list