Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="api.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="api.html" title='Python/C API Reference Manual' /> | |
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="instanceObjects.html" /> | |
13 | <link rel="prev" href="otherObjects.html" /> | |
14 | <link rel="parent" href="otherObjects.html" /> | |
15 | <link rel="next" href="instanceObjects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>7.5.1 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="7.5 Other Objects" | |
25 | href="otherObjects.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="7.5 Other Objects" | |
28 | href="otherObjects.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="7.5.2 Instance Objects" | |
31 | href="instanceObjects.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/C API Reference Manual</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'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></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="otherObjects.html">7.5 Other Objects</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="otherObjects.html">7.5 Other Objects</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="instanceObjects.html">7.5.2 Instance Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION009510000000000000000"></A><A NAME="fileObjects"></A> | |
56 | <BR> | |
57 | 7.5.1 File Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | <a id='l2h-631' xml:id='l2h-631'></a>Python's built-in file objects are implemented entirely on the | |
62 | <tt class="ctype">FILE*</tt> support from the C standard library. This is an | |
63 | implementation detail and may change in future releases of Python. | |
64 | ||
65 | <P> | |
66 | <dl><dt><b><tt class="ctype"><a id='l2h-632' xml:id='l2h-632'>PyFileObject</a></tt></b></dt> | |
67 | <dd> | |
68 | This subtype of <tt class="ctype">PyObject</tt> represents a Python file object. | |
69 | </dl> | |
70 | ||
71 | <P> | |
72 | <dl><dt>PyTypeObject <b><tt id='l2h-633' xml:id='l2h-633' class="cdata">PyFile_Type</tt></b></dt> | |
73 | <dd> | |
74 | This instance of <tt class="ctype">PyTypeObject</tt> represents the Python file | |
75 | type. This is exposed to Python programs as <code>types.FileType</code>. | |
76 | <a id='l2h-635' xml:id='l2h-635'></a></dd></dl> | |
77 | ||
78 | <P> | |
79 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-636' xml:id='l2h-636' class="cfunction">PyFile_Check</tt></b>(</nobr></td><td>PyObject *<var>p</var>)</td></tr></table></dt> | |
80 | <dd> | |
81 | Return true if its argument is a <tt class="ctype">PyFileObject</tt> or a subtype | |
82 | of <tt class="ctype">PyFileObject</tt>. | |
83 | ||
84 | <span class="versionnote">Changed in version 2.2: | |
85 | Allowed subtypes to be accepted.</span> | |
86 | ||
87 | </dd></dl> | |
88 | ||
89 | <P> | |
90 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-637' xml:id='l2h-637' class="cfunction">PyFile_CheckExact</tt></b>(</nobr></td><td>PyObject *<var>p</var>)</td></tr></table></dt> | |
91 | <dd> | |
92 | Return true if its argument is a <tt class="ctype">PyFileObject</tt>, but not a | |
93 | subtype of <tt class="ctype">PyFileObject</tt>. | |
94 | ||
95 | <span class="versionnote">New in version 2.2.</span> | |
96 | ||
97 | </dd></dl> | |
98 | ||
99 | <P> | |
100 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-638' xml:id='l2h-638' class="cfunction">PyFile_FromString</tt></b>(</nobr></td><td>char *<var>filename</var>, char *<var>mode</var>)</td></tr></table></dt> | |
101 | <dd> | |
102 | <div class="refcount-info"> | |
103 | <span class="label">Return value:</span> | |
104 | <span class="value">New reference.</span> | |
105 | </div> | |
106 | On success, return a new file object that is opened on the file | |
107 | given by <var>filename</var>, with a file mode given by <var>mode</var>, where | |
108 | <var>mode</var> has the same semantics as the standard C routine | |
109 | <tt class="cfunction">fopen()</tt><a id='l2h-652' xml:id='l2h-652'></a>. On failure, return <tt class="constant">NULL</tt>. | |
110 | </dd></dl> | |
111 | ||
112 | <P> | |
113 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-639' xml:id='l2h-639' class="cfunction">PyFile_FromFile</tt></b>(</nobr></td><td>FILE *<var>fp</var>, | |
114 | char *<var>name</var>, char *<var>mode</var>, | |
115 | int (*close)(FILE*))</td></tr></table></dt> | |
116 | <dd> | |
117 | <div class="refcount-info"> | |
118 | <span class="label">Return value:</span> | |
119 | <span class="value">New reference.</span> | |
120 | </div> | |
121 | Create a new <tt class="ctype">PyFileObject</tt> from the already-open standard C | |
122 | file pointer, <var>fp</var>. The function <var>close</var> will be called | |
123 | when the file should be closed. Return <tt class="constant">NULL</tt> on failure. | |
124 | </dd></dl> | |
125 | ||
126 | <P> | |
127 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>FILE* <b><tt id='l2h-640' xml:id='l2h-640' class="cfunction">PyFile_AsFile</tt></b>(</nobr></td><td>PyFileObject *<var>p</var>)</td></tr></table></dt> | |
128 | <dd> | |
129 | Return the file object associated with <var>p</var> as a <tt class="ctype">FILE*</tt>. | |
130 | </dd></dl> | |
131 | ||
132 | <P> | |
133 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-641' xml:id='l2h-641' class="cfunction">PyFile_GetLine</tt></b>(</nobr></td><td>PyObject *<var>p</var>, int <var>n</var>)</td></tr></table></dt> | |
134 | <dd> | |
135 | <div class="refcount-info"> | |
136 | <span class="label">Return value:</span> | |
137 | <span class="value">New reference.</span> | |
138 | </div> | |
139 | Equivalent to <code><var>p</var>.readline(<big>[</big><var>n</var><big>]</big>)</code>, this | |
140 | function reads one line from the object <var>p</var>. <var>p</var> may be a | |
141 | file object or any object with a <tt class="method">readline()</tt> method. If | |
142 | <var>n</var> is <code>0</code>, exactly one line is read, regardless of the | |
143 | length of the line. If <var>n</var> is greater than <code>0</code>, no more | |
144 | than <var>n</var> bytes will be read from the file; a partial line can be | |
145 | returned. In both cases, an empty string is returned if the end of | |
146 | the file is reached immediately. If <var>n</var> is less than <code>0</code>, | |
147 | however, one line is read regardless of length, but | |
148 | <tt class="exception">EOFError</tt> is raised if the end of the file is reached | |
149 | immediately. | |
150 | <a id='l2h-643' xml:id='l2h-643'></a></dd></dl> | |
151 | ||
152 | <P> | |
153 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject* <b><tt id='l2h-644' xml:id='l2h-644' class="cfunction">PyFile_Name</tt></b>(</nobr></td><td>PyObject *<var>p</var>)</td></tr></table></dt> | |
154 | <dd> | |
155 | <div class="refcount-info"> | |
156 | <span class="label">Return value:</span> | |
157 | <span class="value">Borrowed reference.</span> | |
158 | </div> | |
159 | Return the name of the file specified by <var>p</var> as a string | |
160 | object. | |
161 | </dd></dl> | |
162 | ||
163 | <P> | |
164 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-645' xml:id='l2h-645' class="cfunction">PyFile_SetBufSize</tt></b>(</nobr></td><td>PyFileObject *<var>p</var>, int <var>n</var>)</td></tr></table></dt> | |
165 | <dd> | |
166 | Available on systems with <tt class="cfunction">setvbuf()</tt><a id='l2h-653' xml:id='l2h-653'></a> | |
167 | only. This should only be called immediately after file object | |
168 | creation. | |
169 | </dd></dl> | |
170 | ||
171 | <P> | |
172 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-646' xml:id='l2h-646' class="cfunction">PyFile_Encoding</tt></b>(</nobr></td><td>PyFileObject *<var>p</var>, char *<var>enc</var>)</td></tr></table></dt> | |
173 | <dd> | |
174 | Set the file's encoding for Unicode output to <var>enc</var>. Return | |
175 | 1 on success and 0 on failure. | |
176 | ||
177 | <span class="versionnote">New in version 2.3.</span> | |
178 | ||
179 | </dd></dl> | |
180 | ||
181 | <P> | |
182 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-647' xml:id='l2h-647' class="cfunction">PyFile_SoftSpace</tt></b>(</nobr></td><td>PyObject *<var>p</var>, int <var>newflag</var>)</td></tr></table></dt> | |
183 | <dd> | |
184 | This function exists for internal use by the interpreter. Set the | |
185 | <tt class="member">softspace</tt> attribute of <var>p</var> to <var>newflag</var> and | |
186 | <a id='l2h-649' xml:id='l2h-649'></a>return the | |
187 | previous value. <var>p</var> does not have to be a file object for this | |
188 | function to work properly; any object is supported (thought its only | |
189 | interesting if the <tt class="member">softspace</tt> attribute can be set). This | |
190 | function clears any errors, and will return <code>0</code> as the previous | |
191 | value if the attribute either does not exist or if there were errors | |
192 | in retrieving it. There is no way to detect errors from this | |
193 | function, but doing so should not be needed. | |
194 | </dd></dl> | |
195 | ||
196 | <P> | |
197 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-650' xml:id='l2h-650' class="cfunction">PyFile_WriteObject</tt></b>(</nobr></td><td>PyObject *<var>obj</var>, PyFileObject *<var>p</var>, | |
198 | int <var>flags</var>)</td></tr></table></dt> | |
199 | <dd> | |
200 | Write object <var>obj</var> to file object <var>p</var>. The only supported | |
201 | flag for <var>flags</var> is | |
202 | <tt class="constant">Py_PRINT_RAW</tt><a id='l2h-654' xml:id='l2h-654'></a>; if given, the | |
203 | <tt class="function">str()</tt> of the object is written instead of the | |
204 | <tt class="function">repr()</tt>. Return <code>0</code> on success or <code>-1</code> on | |
205 | failure; the appropriate exception will be set. | |
206 | </dd></dl> | |
207 | ||
208 | <P> | |
209 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int <b><tt id='l2h-651' xml:id='l2h-651' class="cfunction">PyFile_WriteString</tt></b>(</nobr></td><td>const char *<var>s</var>, PyFileObject *<var>p</var>)</td></tr></table></dt> | |
210 | <dd> | |
211 | Write string <var>s</var> to file object <var>p</var>. Return <code>0</code> on | |
212 | success or <code>-1</code> on failure; the appropriate exception will be | |
213 | set. | |
214 | </dd></dl> | |
215 | ||
216 | <P> | |
217 | ||
218 | <DIV CLASS="navigation"> | |
219 | <div class='online-navigation'> | |
220 | <p></p><hr /> | |
221 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
222 | <tr> | |
223 | <td class='online-navigation'><a rel="prev" title="7.5 Other Objects" | |
224 | href="otherObjects.html"><img src='../icons/previous.png' | |
225 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
226 | <td class='online-navigation'><a rel="parent" title="7.5 Other Objects" | |
227 | href="otherObjects.html"><img src='../icons/up.png' | |
228 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
229 | <td class='online-navigation'><a rel="next" title="7.5.2 Instance Objects" | |
230 | href="instanceObjects.html"><img src='../icons/next.png' | |
231 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
232 | <td align="center" width="100%">Python/C API Reference Manual</td> | |
233 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
234 | href="contents.html"><img src='../icons/contents.png' | |
235 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
236 | <td class='online-navigation'><img src='../icons/blank.png' | |
237 | border='0' height='32' alt='' width='32' /></td> | |
238 | <td class='online-navigation'><a rel="index" title="Index" | |
239 | href="genindex.html"><img src='../icons/index.png' | |
240 | border='0' height='32' alt='Index' width='32' /></A></td> | |
241 | </tr></table> | |
242 | <div class='online-navigation'> | |
243 | <b class="navlabel">Previous:</b> | |
244 | <a class="sectref" rel="prev" href="otherObjects.html">7.5 Other Objects</A> | |
245 | <b class="navlabel">Up:</b> | |
246 | <a class="sectref" rel="parent" href="otherObjects.html">7.5 Other Objects</A> | |
247 | <b class="navlabel">Next:</b> | |
248 | <a class="sectref" rel="next" href="instanceObjects.html">7.5.2 Instance Objects</A> | |
249 | </div> | |
250 | </div> | |
251 | <hr /> | |
252 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
253 | </DIV> | |
254 | <!--End of Navigation Panel--> | |
255 | <ADDRESS> | |
256 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
257 | </ADDRESS> | |
258 | </BODY> | |
259 | </HTML> |