Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / bltin-file-objects.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="typesother.html" />
13<link rel="prev" href="typesmapping.html" />
14<link rel="parent" href="types.html" />
15<link rel="next" href="typesother.html" />
16<meta name='aesop' content='information' />
17<title>2.3.9 File Objects </title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="2.3.8 Mapping Types "
25 href="typesmapping.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="2.3 Built-in Types"
28 href="types.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="2.3.10 Other Built-in Types"
31 href="typesother.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="typesmapping.html">2.3.8 Mapping Types </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="typesother.html">2.3.10 Other Built-in Types</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION004390000000000000000"></A><A NAME="bltin-file-objects"></A>
56<BR>
572.3.9 File Objects
58
59</H2>
60
61<P>
62File objects<a id='l2h-235' xml:id='l2h-235'></a> are implemented using C's <code>stdio</code>
63package and can be created with the built-in constructor
64<tt class="function">file()</tt><a id='l2h-236' xml:id='l2h-236'></a> described in section
65<A href="built-in-funcs.html#built-in-funcs">2.1</A>, ``Built-in Functions.''<A NAME="tex2html11"
66 HREF="#foot3710"><SUP>2.10</SUP></A> File objects are also returned
67by some other built-in functions and methods, such as
68<tt class="function">os.popen()</tt> and <tt class="function">os.fdopen()</tt> and the
69<tt class="method">makefile()</tt> method of socket objects.
70<a id='l2h-257' xml:id='l2h-257'></a>
71
72<P>
73When a file operation fails for an I/O-related reason, the exception
74<tt class="exception">IOError</tt> is raised. This includes situations where the
75operation is not defined for some reason, like <tt class="method">seek()</tt> on a tty
76device or writing a file opened for reading.
77
78<P>
79Files have the following methods:
80
81<P>
82<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
83 <td><nobr><b><tt id='l2h-237' xml:id='l2h-237' class="method">close</tt></b>(</nobr></td>
84 <td><var></var>)</td></tr></table></dt>
85<dd>
86 Close the file. A closed file cannot be read or written any more.
87 Any operation which requires that the file be open will raise a
88 <tt class="exception">ValueError</tt> after the file has been closed. Calling
89 <tt class="method">close()</tt> more than once is allowed.
90</dl>
91
92<P>
93<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
94 <td><nobr><b><tt id='l2h-238' xml:id='l2h-238' class="method">flush</tt></b>(</nobr></td>
95 <td><var></var>)</td></tr></table></dt>
96<dd>
97 Flush the internal buffer, like <code>stdio</code>'s
98 <tt class="cfunction">fflush()</tt>. This may be a no-op on some file-like
99 objects.
100</dl>
101
102<P>
103<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
104 <td><nobr><b><tt id='l2h-239' xml:id='l2h-239' class="method">fileno</tt></b>(</nobr></td>
105 <td><var></var>)</td></tr></table></dt>
106<dd>
107 <a id='l2h-258' xml:id='l2h-258'></a>
108 Return the integer ``file descriptor'' that is used by the
109 underlying implementation to request I/O operations from the
110 operating system. This can be useful for other, lower level
111 interfaces that use file descriptors, such as the
112 <tt class="module"><a href="module-fcntl.html">fcntl</a></tt><a id='l2h-259' xml:id='l2h-259'></a> module or
113 <tt class="function">os.read()</tt> and friends. <span class="note"><b class="label">Note:</b>
114File-like objects
115 which do not have a real file descriptor should <em>not</em> provide
116 this method!</span>
117</dl>
118
119<P>
120<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
121 <td><nobr><b><tt id='l2h-240' xml:id='l2h-240' class="method">isatty</tt></b>(</nobr></td>
122 <td><var></var>)</td></tr></table></dt>
123<dd>
124 Return <code>True</code> if the file is connected to a tty(-like) device, else
125 <code>False</code>. <span class="note"><b class="label">Note:</b>
126If a file-like object is not associated
127 with a real file, this method should <em>not</em> be implemented.</span>
128</dl>
129
130<P>
131<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
132 <td><nobr><b><tt id='l2h-241' xml:id='l2h-241' class="method">next</tt></b>(</nobr></td>
133 <td><var></var>)</td></tr></table></dt>
134<dd>
135A file object is its own iterator, for example <code>iter(<var>f</var>)</code> returns
136<var>f</var> (unless <var>f</var> is closed). When a file is used as an
137iterator, typically in a <tt class="keyword">for</tt> loop (for example,
138<code>for line in f: print line</code>), the <tt class="method">next()</tt> method is
139called repeatedly. This method returns the next input line, or raises
140<tt class="exception">StopIteration</tt> when EOF is hit. In order to make a
141<tt class="keyword">for</tt> loop the most efficient way of looping over the lines of
142a file (a very common operation), the <tt class="method">next()</tt> method uses a
143hidden read-ahead buffer. As a consequence of using a read-ahead
144buffer, combining <tt class="method">next()</tt> with other file methods (like
145<tt class="method">readline()</tt>) does not work right. However, using
146<tt class="method">seek()</tt> to reposition the file to an absolute position will
147flush the read-ahead buffer.
148
149<span class="versionnote">New in version 2.3.</span>
150
151</dl>
152
153<P>
154<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
155 <td><nobr><b><tt id='l2h-242' xml:id='l2h-242' class="method">read</tt></b>(</nobr></td>
156 <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt>
157<dd>
158 Read at most <var>size</var> bytes from the file (less if the read hits
159 EOF before obtaining <var>size</var> bytes). If the <var>size</var>
160 argument is negative or omitted, read all data until EOF is
161 reached. The bytes are returned as a string object. An empty
162 string is returned when EOF is encountered immediately. (For
163 certain files, like ttys, it makes sense to continue reading after
164 an EOF is hit.) Note that this method may call the underlying
165 C function <tt class="cfunction">fread()</tt> more than once in an effort to
166 acquire as close to <var>size</var> bytes as possible. Also note that
167 when in non-blocking mode, less data than what was requested may
168 be returned, even if no <var>size</var> parameter was given.
169</dl>
170
171<P>
172<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
173 <td><nobr><b><tt id='l2h-243' xml:id='l2h-243' class="method">readline</tt></b>(</nobr></td>
174 <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt>
175<dd>
176 Read one entire line from the file. A trailing newline character is
177 kept in the string (but may be absent when a file ends with an
178 incomplete line).<A NAME="tex2html12"
179 HREF="#foot3716"><SUP>2.11</SUP></A> If the <var>size</var> argument is present and
180 non-negative, it is a maximum byte count (including the trailing
181 newline) and an incomplete line may be returned.
182 An empty string is returned <em>only</em> when EOF is encountered
183 immediately. <span class="note"><b class="label">Note:</b>
184Unlike <code>stdio</code>'s <tt class="cfunction">fgets()</tt>, the
185 returned string contains null characters (<code>'&#92;0'</code>) if they
186 occurred in the input.</span>
187</dl>
188
189<P>
190<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
191 <td><nobr><b><tt id='l2h-244' xml:id='l2h-244' class="method">readlines</tt></b>(</nobr></td>
192 <td><var></var><big>[</big><var>sizehint</var><big>]</big><var></var>)</td></tr></table></dt>
193<dd>
194 Read until EOF using <tt class="method">readline()</tt> and return a list containing
195 the lines thus read. If the optional <var>sizehint</var> argument is
196 present, instead of reading up to EOF, whole lines totalling
197 approximately <var>sizehint</var> bytes (possibly after rounding up to an
198 internal buffer size) are read. Objects implementing a file-like
199 interface may choose to ignore <var>sizehint</var> if it cannot be
200 implemented, or cannot be implemented efficiently.
201</dl>
202
203<P>
204<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
205 <td><nobr><b><tt id='l2h-245' xml:id='l2h-245' class="method">xreadlines</tt></b>(</nobr></td>
206 <td><var></var>)</td></tr></table></dt>
207<dd>
208 This method returns the same thing as <code>iter(f)</code>.
209
210<span class="versionnote">New in version 2.1.</span>
211
212 <div class="versionnote"><b>Deprecated since release 2.3.</b>
213Use "<tt class="samp">for <var>line</var> in <var>file</var></tt>" instead.</div><p></p>
214</dl>
215
216<P>
217<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
218 <td><nobr><b><tt id='l2h-246' xml:id='l2h-246' class="method">seek</tt></b>(</nobr></td>
219 <td><var>offset</var><big>[</big><var>, whence</var><big>]</big><var></var>)</td></tr></table></dt>
220<dd>
221 Set the file's current position, like <code>stdio</code>'s <tt class="cfunction">fseek()</tt>.
222 The <var>whence</var> argument is optional and defaults to <code>0</code>
223 (absolute file positioning); other values are <code>1</code> (seek
224 relative to the current position) and <code>2</code> (seek relative to the
225 file's end). There is no return value. Note that if the file is
226 opened for appending (mode <code>'a'</code> or <code>'a+'</code>), any
227 <tt class="method">seek()</tt> operations will be undone at the next write. If the
228 file is only opened for writing in append mode (mode <code>'a'</code>),
229 this method is essentially a no-op, but it remains useful for files
230 opened in append mode with reading enabled (mode <code>'a+'</code>). If the
231 file is opened in text mode (mode <code>'t'</code>), only offsets returned
232 by <tt class="method">tell()</tt> are legal. Use of other offsets causes undefined
233 behavior.
234
235<P>
236Note that not all file objects are seekable.
237</dl>
238
239<P>
240<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
241 <td><nobr><b><tt id='l2h-247' xml:id='l2h-247' class="method">tell</tt></b>(</nobr></td>
242 <td><var></var>)</td></tr></table></dt>
243<dd>
244 Return the file's current position, like <code>stdio</code>'s
245 <tt class="cfunction">ftell()</tt>.
246</dl>
247
248<P>
249<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
250 <td><nobr><b><tt id='l2h-248' xml:id='l2h-248' class="method">truncate</tt></b>(</nobr></td>
251 <td><var></var><big>[</big><var>size</var><big>]</big><var></var>)</td></tr></table></dt>
252<dd>
253 Truncate the file's size. If the optional <var>size</var> argument is
254 present, the file is truncated to (at most) that size. The size
255 defaults to the current position. The current file position is
256 not changed. Note that if a specified size exceeds the file's
257 current size, the result is platform-dependent: possibilities
258 include that file may remain unchanged, increase to the specified
259 size as if zero-filled, or increase to the specified size with
260 undefined new content.
261 Availability: Windows, many <span class="Unix">Unix</span> variants.
262</dl>
263
264<P>
265<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
266 <td><nobr><b><tt id='l2h-249' xml:id='l2h-249' class="method">write</tt></b>(</nobr></td>
267 <td><var>str</var>)</td></tr></table></dt>
268<dd>
269 Write a string to the file. There is no return value. Due to
270 buffering, the string may not actually show up in the file until
271 the <tt class="method">flush()</tt> or <tt class="method">close()</tt> method is called.
272</dl>
273
274<P>
275<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
276 <td><nobr><b><tt id='l2h-250' xml:id='l2h-250' class="method">writelines</tt></b>(</nobr></td>
277 <td><var>sequence</var>)</td></tr></table></dt>
278<dd>
279 Write a sequence of strings to the file. The sequence can be any
280 iterable object producing strings, typically a list of strings.
281 There is no return value.
282 (The name is intended to match <tt class="method">readlines()</tt>;
283 <tt class="method">writelines()</tt> does not add line separators.)
284</dl>
285
286<P>
287Files support the iterator protocol. Each iteration returns the same
288result as <code><var>file</var>.readline()</code>, and iteration ends when the
289<tt class="method">readline()</tt> method returns an empty string.
290
291<P>
292File objects also offer a number of other interesting attributes.
293These are not required for file-like objects, but should be
294implemented if they make sense for the particular object.
295
296<P>
297<dl><dt><b><tt id='l2h-251' xml:id='l2h-251' class="member">closed</tt></b></dt>
298<dd>
299bool indicating the current state of the file object. This is a
300read-only attribute; the <tt class="method">close()</tt> method changes the value.
301It may not be available on all file-like objects.
302</dl>
303
304<P>
305<dl><dt><b><tt id='l2h-252' xml:id='l2h-252' class="member">encoding</tt></b></dt>
306<dd>
307The encoding that this file uses. When Unicode strings are written
308to a file, they will be converted to byte strings using this encoding.
309In addition, when the file is connected to a terminal, the attribute
310gives the encoding that the terminal is likely to use (that
311information might be incorrect if the user has misconfigured the
312terminal). The attribute is read-only and may not be present on
313all file-like objects. It may also be <code>None</code>, in which case
314the file uses the system default encoding for converting Unicode
315strings.
316
317<P>
318
319<span class="versionnote">New in version 2.3.</span>
320
321</dl>
322
323<P>
324<dl><dt><b><tt id='l2h-253' xml:id='l2h-253' class="member">mode</tt></b></dt>
325<dd>
326The I/O mode for the file. If the file was created using the
327<tt class="function">open()</tt> built-in function, this will be the value of the
328<var>mode</var> parameter. This is a read-only attribute and may not be
329present on all file-like objects.
330</dl>
331
332<P>
333<dl><dt><b><tt id='l2h-254' xml:id='l2h-254' class="member">name</tt></b></dt>
334<dd>
335If the file object was created using <tt class="function">open()</tt>, the name of
336the file. Otherwise, some string that indicates the source of the
337file object, of the form "<tt class="samp">&lt;...&gt;</tt>". This is a read-only
338attribute and may not be present on all file-like objects.
339</dl>
340
341<P>
342<dl><dt><b><tt id='l2h-255' xml:id='l2h-255' class="member">newlines</tt></b></dt>
343<dd>
344If Python was built with the <b class="programopt">--with-universal-newlines</b>
345option to <b class="program">configure</b> (the default) this read-only attribute
346exists, and for files opened in
347universal newline read mode it keeps track of the types of newlines
348encountered while reading the file. The values it can take are
349<code>'&#92;r'</code>, <code>'&#92;n'</code>, <code>'&#92;r&#92;n'</code>, <code>None</code> (unknown,
350no newlines read yet) or a tuple containing all the newline
351types seen, to indicate that multiple
352newline conventions were encountered. For files not opened in universal
353newline read mode the value of this attribute will be <code>None</code>.
354</dl>
355
356<P>
357<dl><dt><b><tt id='l2h-256' xml:id='l2h-256' class="member">softspace</tt></b></dt>
358<dd>
359Boolean that indicates whether a space character needs to be printed
360before another value when using the <tt class="keyword">print</tt> statement.
361Classes that are trying to simulate a file object should also have a
362writable <tt class="member">softspace</tt> attribute, which should be initialized to
363zero. This will be automatic for most classes implemented in Python
364(care may be needed for objects that override attribute access); types
365implemented in C will have to provide a writable
366<tt class="member">softspace</tt> attribute.
367<span class="note"><b class="label">Note:</b>
368This attribute is not used to control the
369<tt class="keyword">print</tt> statement, but to allow the implementation of
370<tt class="keyword">print</tt> to keep track of its internal state.</span>
371</dl>
372
373<P>
374<BR><HR><H4>Footnotes</H4>
375<DL>
376<DT><A NAME="foot3710">... Functions.''</A><A
377 href="bltin-file-objects.html#tex2html11"><SUP>2.10</SUP></A></DT>
378<DD><tt class="function">file()</tt>
379is new in Python 2.2. The older built-in <tt class="function">open()</tt> is an
380alias for <tt class="function">file()</tt>.
381
382</DD>
383<DT><A NAME="foot3716">... line).</A><A
384 href="bltin-file-objects.html#tex2html12"><SUP>2.11</SUP></A></DT>
385<DD>
386 The advantage of leaving the newline on is that
387 returning an empty string is then an unambiguous EOF
388 indication. It is also possible (in cases where it might
389 matter, for example, if you
390 want to make an exact copy of a file while scanning its lines)
391 to tell whether the last line of a file ended in a newline
392 or not (yes this happens!).
393
394
395</DD>
396</DL>
397<DIV CLASS="navigation">
398<div class='online-navigation'>
399<p></p><hr />
400<table align="center" width="100%" cellpadding="0" cellspacing="2">
401<tr>
402<td class='online-navigation'><a rel="prev" title="2.3.8 Mapping Types "
403 href="typesmapping.html"><img src='../icons/previous.png'
404 border='0' height='32' alt='Previous Page' width='32' /></A></td>
405<td class='online-navigation'><a rel="parent" title="2.3 Built-in Types"
406 href="types.html"><img src='../icons/up.png'
407 border='0' height='32' alt='Up One Level' width='32' /></A></td>
408<td class='online-navigation'><a rel="next" title="2.3.10 Other Built-in Types"
409 href="typesother.html"><img src='../icons/next.png'
410 border='0' height='32' alt='Next Page' width='32' /></A></td>
411<td align="center" width="100%">Python Library Reference</td>
412<td class='online-navigation'><a rel="contents" title="Table of Contents"
413 href="contents.html"><img src='../icons/contents.png'
414 border='0' height='32' alt='Contents' width='32' /></A></td>
415<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
416 border='0' height='32' alt='Module Index' width='32' /></a></td>
417<td class='online-navigation'><a rel="index" title="Index"
418 href="genindex.html"><img src='../icons/index.png'
419 border='0' height='32' alt='Index' width='32' /></A></td>
420</tr></table>
421<div class='online-navigation'>
422<b class="navlabel">Previous:</b>
423<a class="sectref" rel="prev" href="typesmapping.html">2.3.8 Mapping Types </A>
424<b class="navlabel">Up:</b>
425<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
426<b class="navlabel">Next:</b>
427<a class="sectref" rel="next" href="typesother.html">2.3.10 Other Built-in Types</A>
428</div>
429</div>
430<hr />
431<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
432</DIV>
433<!--End of Navigation Panel-->
434<ADDRESS>
435See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
436</ADDRESS>
437</BODY>
438</HTML>