📄 bltin-file-objects.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>2.1.7.9 File Objects </title>
<META NAME="description" CONTENT="2.1.7.9 File Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="typesinternal.html" tppabs="http://www.python.org/doc/current/lib/typesinternal.html">
<LINK REL="previous" href="bltin-ellipsis-object.html" tppabs="http://www.python.org/doc/current/lib/bltin-ellipsis-object.html">
<LINK REL="up" href="typesother.html" tppabs="http://www.python.org/doc/current/lib/typesother.html">
<LINK REL="next" href="typesinternal.html" tppabs="http://www.python.org/doc/current/lib/typesinternal.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="bltin-ellipsis-object.html" tppabs="http://www.python.org/doc/current/lib/bltin-ellipsis-object.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="typesother.html" tppabs="http://www.python.org/doc/current/lib/typesother.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="typesinternal.html" tppabs="http://www.python.org/doc/current/lib/typesinternal.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="bltin-ellipsis-object.html" tppabs="http://www.python.org/doc/current/lib/bltin-ellipsis-object.html">2.1.7.8 The Ellipsis Object</A>
<b class="navlabel">Up:</b> <a class="sectref" href="typesother.html" tppabs="http://www.python.org/doc/current/lib/typesother.html">2.1.7 Other Built-in Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="typesinternal.html" tppabs="http://www.python.org/doc/current/lib/typesinternal.html">2.1.7.10 Internal Objects</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H3>
<BR>
2.1.7.9 File Objects
</H3>
<P>
File objects are implemented using C's <code>stdio</code> package and can be
created with the built-in function
<tt class="function">open()</tt> described in section
<A href="built-in-funcs.html#built-in-funcs" tppabs="http://www.python.org/doc/current/lib/built-in-funcs.html#built-in-funcs">2.3</A>, ``Built-in Functions.'' They are also returned
by some other built-in functions and methods, e.g.,
<tt class="function">os.popen()</tt> and <tt class="function">os.fdopen()</tt> and the
<tt class="method">makefile()</tt> method of socket objects.
<P>
When a file operation fails for an I/O-related reason, the exception
<tt class="exception">IOError</tt> is raised. This includes situations where the
operation is not defined for some reason, like <tt class="method">seek()</tt> on a tty
device or writing a file opened for reading.
<P>
Files have the following methods:
<P>
<dl><dt><b><a name='l2h-89'><tt class='method'>close</tt></a></b> ()
<dd>
Close the file. A closed file cannot be read or written anymore.
Any operation which requires that the file be open will raise an
<tt class="exception">IOError</tt> after the file has been closed. Calling
<tt class="method">close()</tt> more than once is allowed.
</dl>
<P>
<dl><dt><b><a name='l2h-90'><tt class='method'>flush</tt></a></b> ()
<dd>
Flush the internal buffer, like <code>stdio</code>'s
<tt class="cfunction">fflush()</tt>. This may be a no-op on some file-like
objects.
</dl>
<P>
<dl><dt><b><a name='l2h-91'><tt class='method'>isatty</tt></a></b> ()
<dd>
Return true if the file is connected to a tty(-like) device, else
false. <b>Note:</b> If a file-like object is not associated
with a real file, this method should <i>not</i> be implemented.
</dl>
<P>
<dl><dt><b><a name='l2h-92'><tt class='method'>fileno</tt></a></b> ()
<dd>
Return the integer ``file descriptor'' that is used by the
underlying implementation to request I/O operations from the
operating system. This can be useful for other, lower level
interfaces that use file descriptors, e.g. module
<tt class='module'><a href="module-fcntl.html" tppabs="http://www.python.org/doc/current/lib/module-fcntl.html">fcntl</a></tt> or <tt class="function">os.read()</tt> and
friends. <b>Note:</b> File-like objects which do not have a real
file descriptor should <i>not</i> provide this method!
</dl>
<P>
<dl><dt><b><a name='l2h-93'><tt class='method'>read</tt></a></b> (<big>[</big><var>size</var><big>]</big>)
<dd>
Read at most <var>size</var> bytes from the file (less if the read hits
EOF before obtaining <var>size</var> bytes). 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.) Note that this method may call the underlying
C function <tt class="cfunction">fread()</tt> more than once in an effort to
acquire as close to <var>size</var> bytes as possible.
</dl>
<P>
<dl><dt><b><a name='l2h-94'><tt class='method'>readline</tt></a></b> (<big>[</big><var>size</var><big>]</big>)
<dd>
Read one entire line from the file. A trailing newline character is
kept in the string<A NAME="tex2html7"
HREF="#foot1594"><SUP>2.7</SUP></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 <tt class="cfunction">fgets()</tt>, the
returned string contains null characters (<code>'\0'</code>) if they
occurred in the input.
</dl>
<P>
<dl><dt><b><a name='l2h-95'><tt class='method'>readlines</tt></a></b> (<big>[</big><var>sizehint</var><big>]</big>)
<dd>
Read until EOF using <tt class="method">readline()</tt> and return a list containing
the lines thus read. If the optional <var>sizehint</var> argument is
present, instead of reading up to EOF, whole lines totalling
approximately <var>sizehint</var> bytes (possibly after rounding up to an
internal buffer size) are read. Objects implementing a file-like
interface may choose to ignore <var>sizehint</var> if it cannot be
implemented, or cannot be implemented efficiently.
</dl>
<P>
<dl><dt><b><a name='l2h-96'><tt class='method'>seek</tt></a></b> (<var>offset</var><big>[</big><var>, whence</var><big>]</big>)
<dd>
Set the file's current position, like <code>stdio</code>'s <tt class="cfunction">fseek()</tt>.
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>
<P>
<dl><dt><b><a name='l2h-97'><tt class='method'>tell</tt></a></b> ()
<dd>
Return the file's current position, like <code>stdio</code>'s
<tt class="cfunction">ftell()</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-98'><tt class='method'>truncate</tt></a></b> (<big>[</big><var>size</var><big>]</big>)
<dd>
Truncate the file's size. If the optional <var>size</var> 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 (for example, not all
Unix versions support this operation).
</dl>
<P>
<dl><dt><b><a name='l2h-99'><tt class='method'>write</tt></a></b> (<var>str</var>)
<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 <tt class="method">flush()</tt> or <tt class="method">close()</tt> method is called.
</dl>
<P>
<dl><dt><b><a name='l2h-100'><tt class='method'>writelines</tt></a></b> (<var>list</var>)
<dd>
Write a list of strings to the file. There is no return value.
(The name is intended to match <tt class="method">readlines()</tt>;
<tt class="method">writelines()</tt> does not add line separators.)
</dl>
<P>
File objects also offer a number of other interesting attributes.
These are not required for file-like objects, but should be
implemented if they make sense for the particular object.
<P>
<dl><dt><b><a name='l2h-101'><tt class='member'>closed</tt></a></b>
<dd>
Boolean indicating the current state of the file object. This is a
read-only attribute; the <tt class="method">close()</tt> method changes the value.
It may not be available on all file-like objects.
</dl>
<P>
<dl><dt><b><a name='l2h-102'><tt class='member'>mode</tt></a></b>
<dd>
The I/O mode for the file. If the file was created using the
<tt class="function">open()</tt> built-in function, this will be the value of the
<var>mode</var> parameter. This is a read-only attribute and may not be
present on all file-like objects.
</dl>
<P>
<dl><dt><b><a name='l2h-103'><tt class='member'>name</tt></a></b>
<dd>
If the file object was created using <tt class="function">open()</tt>, the name of
the file. Otherwise, some string that indicates the source of the
file object, of the form "<tt class="samp"><...></tt>". This is a read-only
attribute and may not be present on all file-like objects.
</dl>
<P>
<dl><dt><b><a name='l2h-104'><tt class='member'>softspace</tt></a></b>
<dd>
Boolean that indicates whether a space character needs to be printed
before another value when using the <tt class="keyword">print</tt> statement.
Classes that are trying to simulate a file object should also have a
writable <tt class="member">softspace</tt> attribute, which should be initialized to
zero. This will be automatic for most classes implemented in Python
(care may be needed for objects that override attribute access); types
implemented in C will have to provide a writable
<tt class="member">softspace</tt> attribute.
<b>Note:</b> This attribute is not used to control the
<tt class="keyword">print</tt> statement, but to allow the implementation of
<tt class="keyword">print</tt> to keep track of its internal state.
</dl>
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot1594">... string</A><A NAME="foot1594"
href="bltin-file-objects.html#tex2html7" tppabs="http://www.python.org/doc/current/lib/bltin-file-objects.html#tex2html7"><SUP>2.7</SUP></A>
<DD>
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!).
</DL>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="bltin-ellipsis-object.html" tppabs="http://www.python.org/doc/current/lib/bltin-ellipsis-object.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="typesother.html" tppabs="http://www.python.org/doc/current/lib/typesother.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="typesinternal.html" tppabs="http://www.python.org/doc/current/lib/typesinternal.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="bltin-ellipsis-object.html" tppabs="http://www.python.org/doc/current/lib/bltin-ellipsis-object.html">2.1.7.8 The Ellipsis Object</A>
<b class="navlabel">Up:</b> <a class="sectref" href="typesother.html" tppabs="http://www.python.org/doc/current/lib/typesother.html">2.1.7 Other Built-in Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="typesinternal.html" tppabs="http://www.python.org/doc/current/lib/typesinternal.html">2.1.7.10 Internal Objects</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -