home *** CD-ROM | disk | FTP | other *** search
- <TITLE>File Objects -- Python library reference</TITLE>
- Next: <A HREF="../i/internal_objects" TYPE="Next">Internal Objects</A>
- Prev: <A HREF="../t/the_null_object" TYPE="Prev">The Null Object</A>
- Up: <A HREF="../o/other_built-in_types" TYPE="Up">Other Built-in Types</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H3>2.1.7.8. File Objects</H3>
- File objects are implemented using C's <CODE>stdio</CODE> package and can be
- created with the built-in function <CODE>open()</CODE> described under
- Built-in Functions below. They are also returned by some other
- built-in functions and methods, e.g. <CODE>posix.popen()</CODE> and
- <CODE>posix.fdopen()</CODE> and the <CODE>makefile()</CODE> method of socket
- objects.
- When a file operation fails for an I/O-related reason, the exception
- <CODE>IOError</CODE> is raised. This includes situations where the
- operation is not defined for some reason, like <CODE>seek()</CODE> on a tty
- device or writing a file opened for reading.
- <P>
- Files have the following methods:
- <P>
- <DL><DT><B>close</B> () -- Method on file<DD>
- Close the file. A closed file cannot be read or written anymore.
- </DL>
- <DL><DT><B>flush</B> () -- Method on file<DD>
- Flush the internal buffer, like <CODE>stdio</CODE>'s <CODE>fflush()</CODE>.
- </DL>
- <DL><DT><B>isatty</B> () -- Method on file<DD>
- Return <CODE>1</CODE> if the file is connected to a tty(-like) device, else
- <CODE>0</CODE>.
- </DL>
- <DL><DT><B>read</B> ([<VAR>size</VAR>]) -- Method on file<DD>
- Read at most <VAR>size</VAR> bytes from the file (less if the read hits
- EOF or no more data is immediately available on a pipe, tty or
- similar device). If the <VAR>size</VAR> argument is negative or omitted,
- read all data until EOF is reached. The bytes are returned as a string
- object. An empty string is returned when EOF is encountered
- immediately. (For certain files, like ttys, it makes sense to
- continue reading after an EOF is hit.)
- </DL>
- <DL><DT><B>readline</B> ([<VAR>size</VAR>]) -- Method on file<DD>
- Read one entire line from the file. A trailing newline character is
- kept in the string<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
- (but may be absent when a file ends with an
- incomplete line). If the <VAR>size</VAR> argument is present and
- non-negative, it is a maximum byte count (including the trailing
- newline) and an incomplete line may be returned.
- An empty string is returned when EOF is hit
- immediately. Note: unlike <CODE>stdio</CODE>'s <CODE>fgets()</CODE>, the returned
- string contains null characters (<CODE>'\0'</CODE>) if they occurred in the
- input.
- </DL>
- <DL><DT><B>readlines</B> () -- Method on file<DD>
- Read until EOF using <CODE>readline()</CODE> and return a list containing
- the lines thus read.
- </DL>
- <DL><DT><B>seek</B> (<VAR>offset</VAR>, <VAR>whence</VAR>) -- Method on file<DD>
- Set the file's current position, like <CODE>stdio</CODE>'s <CODE>fseek()</CODE>.
- The <VAR>whence</VAR> argument is optional and defaults to <CODE>0</CODE>
- (absolute file positioning); other values are <CODE>1</CODE> (seek
- relative to the current position) and <CODE>2</CODE> (seek relative to the
- file's end). There is no return value.
- </DL>
- <DL><DT><B>tell</B> () -- Method on file<DD>
- Return the file's current position, like <CODE>stdio</CODE>'s <CODE>ftell()</CODE>.
- </DL>
- <DL><DT><B>truncate</B> ([<VAR>size</VAR>]) -- Method on file<DD>
- Truncate the file's size. If the optional size argument present, the
- file is truncated to (at most) that size. The size defaults to the
- current position. Availability of this function depends on the
- operating system version (e.g., not all UNIX versions support this
- operation).
- </DL>
- <DL><DT><B>write</B> (<VAR>str</VAR>) -- Method on file<DD>
- Write a string to the file. There is no return value. Note: due to
- buffering, the string may not actually show up in the file until
- the <CODE>flush()</CODE> or <CODE>close()</CODE> method is called.
- </DL>
- <DL><DT><B>writelines</B> (<VAR>list</VAR>) -- Method on file<DD>
- Write a list of strings to the file. There is no return value.
- (The name is intended to match <CODE>readlines</CODE>; <CODE>writelines</CODE>
- does not add line separators.)
- </DL>
- <H2>---------- Footnotes ----------</H2>
- <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
- The advantage of leaving the newline on is that an empty string
- can be returned to mean EOF without being ambiguous. Another
- advantage is that (in cases where it might matter, e.g. if you
- want to make an exact copy of a file while scanning its lines)
- you can tell whether the last line of a file ended in a newline
- or not (yes this happens!).<P>
-