Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / os-file-dir.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="os-process.html" />
13<link rel="prev" href="os-fd-ops.html" />
14<link rel="parent" href="module-os.html" />
15<link rel="next" href="os-process.html" />
16<meta name='aesop' content='information' />
17<title>6.1.4 Files and Directories </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="6.1.3 File Descriptor Operations"
25 href="os-fd-ops.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="6.1 os "
28 href="module-os.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="6.1.5 Process Management"
31 href="os-process.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="os-fd-ops.html">6.1.3 File Descriptor Operations</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-os.html">6.1 os </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="os-process.html">6.1.5 Process Management</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION008140000000000000000"></A><A NAME="os-file-dir"></A>
56<BR>
576.1.4 Files and Directories
58</H2>
59
60<P>
61<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
62 <td><nobr><b><tt id='l2h-1598' xml:id='l2h-1598' class="function">access</tt></b>(</nobr></td>
63 <td><var>path, mode</var>)</td></tr></table></dt>
64<dd>
65Use the real uid/gid to test for access to <var>path</var>. Note that most
66operations will use the effective uid/gid, therefore this routine can
67be used in a suid/sgid environment to test if the invoking user has the
68specified access to <var>path</var>. <var>mode</var> should be <tt class="constant">F_OK</tt>
69to test the existence of <var>path</var>, or it can be the inclusive OR of
70one or more of <tt class="constant">R_OK</tt>, <tt class="constant">W_OK</tt>, and <tt class="constant">X_OK</tt> to
71test permissions. Return <tt class="constant">True</tt> if access is allowed,
72<tt class="constant">False</tt> if not.
73See the <span class="Unix">Unix</span> man page <span class="manpage"><i>access</i>(2)</span> for more information.
74Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
75
76<P>
77<span class="note"><b class="label">Note:</b>
78Using <tt class="function">access()</tt> to check if a user is authorized to e.g.
79open a file before actually doing so using <tt class="function">open()</tt> creates a
80security hole, because the user might exploit the short time interval
81between checking and opening the file to manipulate it.</span>
82</dl>
83
84<P>
85<dl><dt><b><tt id='l2h-1599' xml:id='l2h-1599'>F_OK</tt></b></dt>
86<dd>
87 Value to pass as the <var>mode</var> parameter of <tt class="function">access()</tt> to
88 test the existence of <var>path</var>.
89</dd></dl>
90
91<P>
92<dl><dt><b><tt id='l2h-1600' xml:id='l2h-1600'>R_OK</tt></b></dt>
93<dd>
94 Value to include in the <var>mode</var> parameter of <tt class="function">access()</tt>
95 to test the readability of <var>path</var>.
96</dd></dl>
97
98<P>
99<dl><dt><b><tt id='l2h-1601' xml:id='l2h-1601'>W_OK</tt></b></dt>
100<dd>
101 Value to include in the <var>mode</var> parameter of <tt class="function">access()</tt>
102 to test the writability of <var>path</var>.
103</dd></dl>
104
105<P>
106<dl><dt><b><tt id='l2h-1602' xml:id='l2h-1602'>X_OK</tt></b></dt>
107<dd>
108 Value to include in the <var>mode</var> parameter of <tt class="function">access()</tt>
109 to determine if <var>path</var> can be executed.
110</dd></dl>
111
112<P>
113<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
114 <td><nobr><b><tt id='l2h-1603' xml:id='l2h-1603' class="function">chdir</tt></b>(</nobr></td>
115 <td><var>path</var>)</td></tr></table></dt>
116<dd>
117<a id='l2h-1639' xml:id='l2h-1639'></a>
118Change the current working directory to <var>path</var>.
119Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
120</dl>
121
122<P>
123<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
124 <td><nobr><b><tt id='l2h-1604' xml:id='l2h-1604' class="function">fchdir</tt></b>(</nobr></td>
125 <td><var>fd</var>)</td></tr></table></dt>
126<dd>
127Change the current working directory to the directory represented by
128the file descriptor <var>fd</var>. The descriptor must refer to an opened
129directory, not an open file.
130Availability: <span class="Unix">Unix</span>.
131
132<span class="versionnote">New in version 2.3.</span>
133
134</dl>
135
136<P>
137<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
138 <td><nobr><b><tt id='l2h-1605' xml:id='l2h-1605' class="function">getcwd</tt></b>(</nobr></td>
139 <td><var></var>)</td></tr></table></dt>
140<dd>
141Return a string representing the current working directory.
142Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
143</dl>
144
145<P>
146<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
147 <td><nobr><b><tt id='l2h-1606' xml:id='l2h-1606' class="function">getcwdu</tt></b>(</nobr></td>
148 <td><var></var>)</td></tr></table></dt>
149<dd>
150Return a Unicode object representing the current working directory.
151Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
152
153<span class="versionnote">New in version 2.3.</span>
154
155</dl>
156
157<P>
158<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
159 <td><nobr><b><tt id='l2h-1607' xml:id='l2h-1607' class="function">chroot</tt></b>(</nobr></td>
160 <td><var>path</var>)</td></tr></table></dt>
161<dd>
162Change the root directory of the current process to <var>path</var>.
163Availability: Macintosh, <span class="Unix">Unix</span>.
164
165<span class="versionnote">New in version 2.2.</span>
166
167</dl>
168
169<P>
170<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
171 <td><nobr><b><tt id='l2h-1608' xml:id='l2h-1608' class="function">chmod</tt></b>(</nobr></td>
172 <td><var>path, mode</var>)</td></tr></table></dt>
173<dd>
174Change the mode of <var>path</var> to the numeric <var>mode</var>.
175<var>mode</var> may take one of the following values
176(as defined in the <tt class="module">stat</tt> module):
177
178<UL>
179<LI><code>S_ISUID</code>
180</LI>
181<LI><code>S_ISGID</code>
182</LI>
183<LI><code>S_ENFMT</code>
184</LI>
185<LI><code>S_ISVTX</code>
186</LI>
187<LI><code>S_IREAD</code>
188</LI>
189<LI><code>S_IWRITE</code>
190</LI>
191<LI><code>S_IEXEC</code>
192</LI>
193<LI><code>S_IRWXU</code>
194</LI>
195<LI><code>S_IRUSR</code>
196</LI>
197<LI><code>S_IWUSR</code>
198</LI>
199<LI><code>S_IXUSR</code>
200</LI>
201<LI><code>S_IRWXG</code>
202</LI>
203<LI><code>S_IRGRP</code>
204</LI>
205<LI><code>S_IWGRP</code>
206</LI>
207<LI><code>S_IXGRP</code>
208</LI>
209<LI><code>S_IRWXO</code>
210</LI>
211<LI><code>S_IROTH</code>
212</LI>
213<LI><code>S_IWOTH</code>
214</LI>
215<LI><code>S_IXOTH</code>
216</LI>
217</UL>
218Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
219
220<P>
221<span class="note"><b class="label">Note:</b>
222Although Windows supports <tt class="function">chmod()</tt>, you can only
223set the file's read-only flag with it (via the <code>S_IWRITE</code>
224and <code>S_IREAD</code> constants or a corresponding integer value).
225All other bits are ignored.</span>
226</dl>
227
228<P>
229<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
230 <td><nobr><b><tt id='l2h-1609' xml:id='l2h-1609' class="function">chown</tt></b>(</nobr></td>
231 <td><var>path, uid, gid</var>)</td></tr></table></dt>
232<dd>
233Change the owner and group id of <var>path</var> to the numeric <var>uid</var>
234and <var>gid</var>. To leave one of the ids unchanged, set it to -1.
235Availability: Macintosh, <span class="Unix">Unix</span>.
236</dl>
237
238<P>
239<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
240 <td><nobr><b><tt id='l2h-1610' xml:id='l2h-1610' class="function">lchown</tt></b>(</nobr></td>
241 <td><var>path, uid, gid</var>)</td></tr></table></dt>
242<dd>
243Change the owner and group id of <var>path</var> to the numeric <var>uid</var>
244and gid. This function will not follow symbolic links.
245Availability: Macintosh, <span class="Unix">Unix</span>.
246
247<span class="versionnote">New in version 2.3.</span>
248
249</dl>
250
251<P>
252<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
253 <td><nobr><b><tt id='l2h-1611' xml:id='l2h-1611' class="function">link</tt></b>(</nobr></td>
254 <td><var>src, dst</var>)</td></tr></table></dt>
255<dd>
256Create a hard link pointing to <var>src</var> named <var>dst</var>.
257Availability: Macintosh, <span class="Unix">Unix</span>.
258</dl>
259
260<P>
261<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
262 <td><nobr><b><tt id='l2h-1612' xml:id='l2h-1612' class="function">listdir</tt></b>(</nobr></td>
263 <td><var>path</var>)</td></tr></table></dt>
264<dd>
265Return a list containing the names of the entries in the directory.
266The list is in arbitrary order. It does not include the special
267entries <code>'.'</code> and <code>'..'</code> even if they are present in the
268directory.
269Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
270
271<P>
272
273<span class="versionnote">Changed in version 2.3:
274On Windows NT/2k/XP and Unix, if <var>path</var> is a Unicode
275object, the result will be a list of Unicode objects..</span>
276
277</dl>
278
279<P>
280<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
281 <td><nobr><b><tt id='l2h-1613' xml:id='l2h-1613' class="function">lstat</tt></b>(</nobr></td>
282 <td><var>path</var>)</td></tr></table></dt>
283<dd>
284Like <tt class="function">stat()</tt>, but do not follow symbolic links.
285Availability: Macintosh, <span class="Unix">Unix</span>.
286</dl>
287
288<P>
289<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
290 <td><nobr><b><tt id='l2h-1614' xml:id='l2h-1614' class="function">mkfifo</tt></b>(</nobr></td>
291 <td><var>path</var><big>[</big><var>, mode</var><big>]</big><var></var>)</td></tr></table></dt>
292<dd>
293Create a FIFO (a named pipe) named <var>path</var> with numeric mode
294<var>mode</var>. The default <var>mode</var> is <code>0666</code> (octal). The current
295umask value is first masked out from the mode.
296Availability: Macintosh, <span class="Unix">Unix</span>.
297
298<P>
299FIFOs are pipes that can be accessed like regular files. FIFOs exist
300until they are deleted (for example with <tt class="function">os.unlink()</tt>).
301Generally, FIFOs are used as rendezvous between ``client'' and
302``server'' type processes: the server opens the FIFO for reading, and
303the client opens it for writing. Note that <tt class="function">mkfifo()</tt>
304doesn't open the FIFO -- it just creates the rendezvous point.
305</dl>
306
307<P>
308<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
309 <td><nobr><b><tt id='l2h-1615' xml:id='l2h-1615' class="function">mknod</tt></b>(</nobr></td>
310 <td><var>path</var><big>[</big><var>, mode=0600, device</var><big>]</big><var></var>)</td></tr></table></dt>
311<dd>
312Create a filesystem node (file, device special file or named pipe)
313named filename. <var>mode</var> specifies both the permissions to use and
314the type of node to be created, being combined (bitwise OR) with one
315of S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO (those constants are
316available in <tt class="module">stat</tt>). For S_IFCHR and S_IFBLK, <var>device</var>
317defines the newly created device special file (probably using
318<tt class="function">os.makedev()</tt>), otherwise it is ignored.
319
320<span class="versionnote">New in version 2.3.</span>
321
322</dl>
323
324<P>
325<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
326 <td><nobr><b><tt id='l2h-1616' xml:id='l2h-1616' class="function">major</tt></b>(</nobr></td>
327 <td><var>device</var>)</td></tr></table></dt>
328<dd>
329Extracts a device major number from a raw device number.
330
331<span class="versionnote">New in version 2.3.</span>
332
333</dl>
334
335<P>
336<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
337 <td><nobr><b><tt id='l2h-1617' xml:id='l2h-1617' class="function">minor</tt></b>(</nobr></td>
338 <td><var>device</var>)</td></tr></table></dt>
339<dd>
340Extracts a device minor number from a raw device number.
341
342<span class="versionnote">New in version 2.3.</span>
343
344</dl>
345
346<P>
347<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
348 <td><nobr><b><tt id='l2h-1618' xml:id='l2h-1618' class="function">makedev</tt></b>(</nobr></td>
349 <td><var>major, minor</var>)</td></tr></table></dt>
350<dd>
351Composes a raw device number from the major and minor device numbers.
352
353<span class="versionnote">New in version 2.3.</span>
354
355</dl>
356
357<P>
358<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
359 <td><nobr><b><tt id='l2h-1619' xml:id='l2h-1619' class="function">mkdir</tt></b>(</nobr></td>
360 <td><var>path</var><big>[</big><var>, mode</var><big>]</big><var></var>)</td></tr></table></dt>
361<dd>
362Create a directory named <var>path</var> with numeric mode <var>mode</var>.
363The default <var>mode</var> is <code>0777</code> (octal). On some systems,
364<var>mode</var> is ignored. Where it is used, the current umask value is
365first masked out.
366Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
367</dl>
368
369<P>
370<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
371 <td><nobr><b><tt id='l2h-1620' xml:id='l2h-1620' class="function">makedirs</tt></b>(</nobr></td>
372 <td><var>path</var><big>[</big><var>, mode</var><big>]</big><var></var>)</td></tr></table></dt>
373<dd>
374Recursive directory creation function.<a id='l2h-1640' xml:id='l2h-1640'></a>
375Like <tt class="function">mkdir()</tt>,
376but makes all intermediate-level directories needed to contain the
377leaf directory. Throws an <tt class="exception">error</tt> exception if the leaf
378directory already exists or cannot be created. The default <var>mode</var>
379is <code>0777</code> (octal). This function does not properly handle UNC
380paths (only relevant on Windows systems; Universal Naming Convention
381paths are those that use the `<code>&#92;&#92;host&#92;path</code>' syntax).
382
383<span class="versionnote">New in version 1.5.2.</span>
384
385</dl>
386
387<P>
388<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
389 <td><nobr><b><tt id='l2h-1621' xml:id='l2h-1621' class="function">pathconf</tt></b>(</nobr></td>
390 <td><var>path, name</var>)</td></tr></table></dt>
391<dd>
392Return system configuration information relevant to a named file.
393<var>name</var> specifies the configuration value to retrieve; it may be a
394string which is the name of a defined system value; these names are
395specified in a number of standards (POSIX.1, <span class="Unix">Unix</span> 95, <span class="Unix">Unix</span> 98, and
396others). Some platforms define additional names as well. The names
397known to the host operating system are given in the
398<code>pathconf_names</code> dictionary. For configuration variables not
399included in that mapping, passing an integer for <var>name</var> is also
400accepted.
401Availability: Macintosh, <span class="Unix">Unix</span>.
402
403<P>
404If <var>name</var> is a string and is not known, <tt class="exception">ValueError</tt> is
405raised. If a specific value for <var>name</var> is not supported by the
406host system, even if it is included in <code>pathconf_names</code>, an
407<tt class="exception">OSError</tt> is raised with <tt class="constant">errno.EINVAL</tt> for the
408error number.
409</dl>
410
411<P>
412<dl><dt><b><tt id='l2h-1622' xml:id='l2h-1622'>pathconf_names</tt></b></dt>
413<dd>
414Dictionary mapping names accepted by <tt class="function">pathconf()</tt> and
415<tt class="function">fpathconf()</tt> to the integer values defined for those names
416by the host operating system. This can be used to determine the set
417of names known to the system.
418Availability: Macintosh, <span class="Unix">Unix</span>.
419</dd></dl>
420
421<P>
422<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
423 <td><nobr><b><tt id='l2h-1623' xml:id='l2h-1623' class="function">readlink</tt></b>(</nobr></td>
424 <td><var>path</var>)</td></tr></table></dt>
425<dd>
426Return a string representing the path to which the symbolic link
427points. The result may be either an absolute or relative pathname; if
428it is relative, it may be converted to an absolute pathname using
429<code>os.path.join(os.path.dirname(<var>path</var>), <var>result</var>)</code>.
430Availability: Macintosh, <span class="Unix">Unix</span>.
431</dl>
432
433<P>
434<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
435 <td><nobr><b><tt id='l2h-1624' xml:id='l2h-1624' class="function">remove</tt></b>(</nobr></td>
436 <td><var>path</var>)</td></tr></table></dt>
437<dd>
438Remove the file <var>path</var>. If <var>path</var> is a directory,
439<tt class="exception">OSError</tt> is raised; see <tt class="function">rmdir()</tt> below to remove
440a directory. This is identical to the <tt class="function">unlink()</tt> function
441documented below. On Windows, attempting to remove a file that is in
442use causes an exception to be raised; on <span class="Unix">Unix</span>, the directory entry is
443removed but the storage allocated to the file is not made available
444until the original file is no longer in use.
445Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
446</dl>
447
448<P>
449<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
450 <td><nobr><b><tt id='l2h-1625' xml:id='l2h-1625' class="function">removedirs</tt></b>(</nobr></td>
451 <td><var>path</var>)</td></tr></table></dt>
452<dd>
453<a id='l2h-1641' xml:id='l2h-1641'></a>
454Removes directories recursively. Works like
455<tt class="function">rmdir()</tt> except that, if the leaf directory is
456successfully removed, directories corresponding to rightmost path
457segments will be pruned way until either the whole path is consumed or
458an error is raised (which is ignored, because it generally means that
459a parent directory is not empty). Throws an <tt class="exception">error</tt>
460exception if the leaf directory could not be successfully removed.
461
462<span class="versionnote">New in version 1.5.2.</span>
463
464</dl>
465
466<P>
467<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
468 <td><nobr><b><tt id='l2h-1626' xml:id='l2h-1626' class="function">rename</tt></b>(</nobr></td>
469 <td><var>src, dst</var>)</td></tr></table></dt>
470<dd>
471Rename the file or directory <var>src</var> to <var>dst</var>. If <var>dst</var> is
472a directory, <tt class="exception">OSError</tt> will be raised. On <span class="Unix">Unix</span>, if
473<var>dst</var> exists and is a file, it will be removed silently if the
474user has permission. The operation may fail on some <span class="Unix">Unix</span> flavors
475if <var>src</var> and <var>dst</var> are on different filesystems. If
476successful, the renaming will be an atomic operation (this is a
477POSIX requirement). On Windows, if <var>dst</var> already exists,
478<tt class="exception">OSError</tt> will be raised even if it is a file; there may be
479no way to implement an atomic rename when <var>dst</var> names an existing
480file.
481Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
482</dl>
483
484<P>
485<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
486 <td><nobr><b><tt id='l2h-1627' xml:id='l2h-1627' class="function">renames</tt></b>(</nobr></td>
487 <td><var>old, new</var>)</td></tr></table></dt>
488<dd>
489Recursive directory or file renaming function.
490Works like <tt class="function">rename()</tt>, except creation of any intermediate
491directories needed to make the new pathname good is attempted first.
492After the rename, directories corresponding to rightmost path segments
493of the old name will be pruned away using <tt class="function">removedirs()</tt>.
494
495<span class="versionnote">New in version 1.5.2.</span>
496
497<P>
498<div class="note"><b class="label">Note:</b>
499This function can fail with the new directory structure made if
500you lack permissions needed to remove the leaf directory or file.
501</div>
502</dl>
503
504<P>
505<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
506 <td><nobr><b><tt id='l2h-1628' xml:id='l2h-1628' class="function">rmdir</tt></b>(</nobr></td>
507 <td><var>path</var>)</td></tr></table></dt>
508<dd>
509Remove the directory <var>path</var>.
510Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
511</dl>
512
513<P>
514<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
515 <td><nobr><b><tt id='l2h-1629' xml:id='l2h-1629' class="function">stat</tt></b>(</nobr></td>
516 <td><var>path</var>)</td></tr></table></dt>
517<dd>
518Perform a <tt class="cfunction">stat()</tt> system call on the given path. The
519return value is an object whose attributes correspond to the members of
520the <tt class="ctype">stat</tt> structure, namely:
521<tt class="member">st_mode</tt> (protection bits),
522<tt class="member">st_ino</tt> (inode number),
523<tt class="member">st_dev</tt> (device),
524<tt class="member">st_nlink</tt> (number of hard links),
525<tt class="member">st_uid</tt> (user ID of owner),
526<tt class="member">st_gid</tt> (group ID of owner),
527<tt class="member">st_size</tt> (size of file, in bytes),
528<tt class="member">st_atime</tt> (time of most recent access),
529<tt class="member">st_mtime</tt> (time of most recent content modification),
530<tt class="member">st_ctime</tt>
531(platform dependent; time of most recent metadata change on <span class="Unix">Unix</span>, or
532the time of creation on Windows).
533
534<P>
535
536<span class="versionnote">Changed in version 2.3:
537If <tt class="function">stat_float_times</tt> returns true, the time
538values are floats, measuring seconds. Fractions of a second may be
539reported if the system supports that. On Mac OS, the times are always
540floats. See <tt class="function">stat_float_times</tt> for further discussion. .</span>
541
542<P>
543On some Unix systems (such as Linux), the following attributes may
544also be available:
545<tt class="member">st_blocks</tt> (number of blocks allocated for file),
546<tt class="member">st_blksize</tt> (filesystem blocksize),
547<tt class="member">st_rdev</tt> (type of device if an inode device).
548
549<P>
550On Mac OS systems, the following attributes may also be available:
551<tt class="member">st_rsize</tt>,
552<tt class="member">st_creator</tt>,
553<tt class="member">st_type</tt>.
554
555<P>
556On RISCOS systems, the following attributes are also available:
557<tt class="member">st_ftype</tt> (file type),
558<tt class="member">st_attrs</tt> (attributes),
559<tt class="member">st_obtype</tt> (object type).
560
561<P>
562For backward compatibility, the return value of <tt class="function">stat()</tt> is
563also accessible as a tuple of at least 10 integers giving the most
564important (and portable) members of the <tt class="ctype">stat</tt> structure, in the
565order
566<tt class="member">st_mode</tt>,
567<tt class="member">st_ino</tt>,
568<tt class="member">st_dev</tt>,
569<tt class="member">st_nlink</tt>,
570<tt class="member">st_uid</tt>,
571<tt class="member">st_gid</tt>,
572<tt class="member">st_size</tt>,
573<tt class="member">st_atime</tt>,
574<tt class="member">st_mtime</tt>,
575<tt class="member">st_ctime</tt>.
576More items may be added at the end by some implementations.
577The standard module <tt class="module"><a href="module-stat.html">stat</a></tt><a id='l2h-1642' xml:id='l2h-1642'></a> defines
578functions and constants that are useful for extracting information
579from a <tt class="ctype">stat</tt> structure.
580(On Windows, some items are filled with dummy values.)
581
582<P>
583<span class="note"><b class="label">Note:</b>
584The exact meaning and resolution of the <tt class="member">st_atime</tt>,
585 <tt class="member">st_mtime</tt>, and <tt class="member">st_ctime</tt> members depends on the
586 operating system and the file system. For example, on Windows systems
587 using the FAT or FAT32 file systems, <tt class="member">st_mtime</tt> has 2-second
588 resolution, and <tt class="member">st_atime</tt> has only 1-day resolution. See
589 your operating system documentation for details.</span>
590
591<P>
592Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
593
594<P>
595
596<span class="versionnote">Changed in version 2.2:
597Added access to values as attributes of the returned object.</span>
598
599</dl>
600
601<P>
602<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
603 <td><nobr><b><tt id='l2h-1630' xml:id='l2h-1630' class="function">stat_float_times</tt></b>(</nobr></td>
604 <td><var></var><big>[</big><var>newvalue</var><big>]</big><var></var>)</td></tr></table></dt>
605<dd>
606Determine whether <tt class="class">stat_result</tt> represents time stamps as float
607objects. If newval is True, future calls to stat() return floats, if
608it is False, future calls return ints. If newval is omitted, return
609the current setting.
610
611<P>
612For compatibility with older Python versions, accessing
613<tt class="class">stat_result</tt> as a tuple always returns integers. For
614compatibility with Python 2.2, accessing the time stamps by field name
615also returns integers. Applications that want to determine the
616fractions of a second in a time stamp can use this function to have
617time stamps represented as floats. Whether they will actually observe
618non-zero fractions depends on the system.
619
620<P>
621Future Python releases will change the default of this setting;
622applications that cannot deal with floating point time stamps can then
623use this function to turn the feature off.
624
625<P>
626It is recommended that this setting is only changed at program startup
627time in the <var>__main__</var> module; libraries should never change this
628setting. If an application uses a library that works incorrectly if
629floating point time stamps are processed, this application should turn
630the feature off until the library has been corrected.
631
632<P>
633</dl>
634
635<P>
636<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
637 <td><nobr><b><tt id='l2h-1631' xml:id='l2h-1631' class="function">statvfs</tt></b>(</nobr></td>
638 <td><var>path</var>)</td></tr></table></dt>
639<dd>
640Perform a <tt class="cfunction">statvfs()</tt> system call on the given path. The
641return value is an object whose attributes describe the filesystem on
642the given path, and correspond to the members of the
643<tt class="ctype">statvfs</tt> structure, namely:
644<tt class="member">f_frsize</tt>,
645<tt class="member">f_blocks</tt>,
646<tt class="member">f_bfree</tt>,
647<tt class="member">f_bavail</tt>,
648<tt class="member">f_files</tt>,
649<tt class="member">f_ffree</tt>,
650<tt class="member">f_favail</tt>,
651<tt class="member">f_flag</tt>,
652<tt class="member">f_namemax</tt>.
653Availability: <span class="Unix">Unix</span>.
654
655<P>
656For backward compatibility, the return value is also accessible as a
657tuple whose values correspond to the attributes, in the order given above.
658The standard module <tt class="module"><a href="module-statvfs.html">statvfs</a></tt><a id='l2h-1643' xml:id='l2h-1643'></a>
659defines constants that are useful for extracting information
660from a <tt class="ctype">statvfs</tt> structure when accessing it as a sequence; this
661remains useful when writing code that needs to work with versions of
662Python that don't support accessing the fields as attributes.
663
664<P>
665
666<span class="versionnote">Changed in version 2.2:
667Added access to values as attributes of the returned object.</span>
668
669</dl>
670
671<P>
672<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
673 <td><nobr><b><tt id='l2h-1632' xml:id='l2h-1632' class="function">symlink</tt></b>(</nobr></td>
674 <td><var>src, dst</var>)</td></tr></table></dt>
675<dd>
676Create a symbolic link pointing to <var>src</var> named <var>dst</var>.
677Availability: <span class="Unix">Unix</span>.
678</dl>
679
680<P>
681<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
682 <td><nobr><b><tt id='l2h-1633' xml:id='l2h-1633' class="function">tempnam</tt></b>(</nobr></td>
683 <td><var></var><big>[</big><var>dir</var><big>[</big><var>, prefix</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
684<dd>
685Return a unique path name that is reasonable for creating a temporary
686file. This will be an absolute path that names a potential directory
687entry in the directory <var>dir</var> or a common location for temporary
688files if <var>dir</var> is omitted or <code>None</code>. If given and not
689<code>None</code>, <var>prefix</var> is used to provide a short prefix to the
690filename. Applications are responsible for properly creating and
691managing files created using paths returned by <tt class="function">tempnam()</tt>;
692no automatic cleanup is provided.
693On <span class="Unix">Unix</span>, the environment variable <a class="envvar" id='l2h-1644' xml:id='l2h-1644'>TMPDIR</a> overrides
694<var>dir</var>, while on Windows the <a class="envvar" id='l2h-1645' xml:id='l2h-1645'>TMP</a> is used. The specific
695behavior of this function depends on the C library implementation;
696some aspects are underspecified in system documentation.
697<span class="warning"><b class="label">Warning:</b>
698Use of <tt class="function">tempnam()</tt> is vulnerable to symlink attacks;
699consider using <tt class="function">tmpfile()</tt> (section <A href="os-newstreams.html#os-newstreams">6.1.2</A>)
700instead.</span> Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
701</dl>
702
703<P>
704<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
705 <td><nobr><b><tt id='l2h-1634' xml:id='l2h-1634' class="function">tmpnam</tt></b>(</nobr></td>
706 <td><var></var>)</td></tr></table></dt>
707<dd>
708Return a unique path name that is reasonable for creating a temporary
709file. This will be an absolute path that names a potential directory
710entry in a common location for temporary files. Applications are
711responsible for properly creating and managing files created using
712paths returned by <tt class="function">tmpnam()</tt>; no automatic cleanup is
713provided.
714<span class="warning"><b class="label">Warning:</b>
715Use of <tt class="function">tmpnam()</tt> is vulnerable to symlink attacks;
716consider using <tt class="function">tmpfile()</tt> (section <A href="os-newstreams.html#os-newstreams">6.1.2</A>)
717instead.</span> Availability: <span class="Unix">Unix</span>, Windows. This function probably
718shouldn't be used on Windows, though: Microsoft's implementation of
719<tt class="function">tmpnam()</tt> always creates a name in the root directory of the
720current drive, and that's generally a poor location for a temp file
721(depending on privileges, you may not even be able to open a file
722using this name).
723</dl>
724
725<P>
726<dl><dt><b><tt id='l2h-1635' xml:id='l2h-1635'>TMP_MAX</tt></b></dt>
727<dd>
728The maximum number of unique names that <tt class="function">tmpnam()</tt> will
729generate before reusing names.
730</dd></dl>
731
732<P>
733<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
734 <td><nobr><b><tt id='l2h-1636' xml:id='l2h-1636' class="function">unlink</tt></b>(</nobr></td>
735 <td><var>path</var>)</td></tr></table></dt>
736<dd>
737Remove the file <var>path</var>. This is the same function as
738<tt class="function">remove()</tt>; the <tt class="function">unlink()</tt> name is its traditional
739<span class="Unix">Unix</span> name.
740Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
741</dl>
742
743<P>
744<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
745 <td><nobr><b><tt id='l2h-1637' xml:id='l2h-1637' class="function">utime</tt></b>(</nobr></td>
746 <td><var>path, times</var>)</td></tr></table></dt>
747<dd>
748Set the access and modified times of the file specified by <var>path</var>.
749If <var>times</var> is <code>None</code>, then the file's access and modified
750times are set to the current time. Otherwise, <var>times</var> must be a
7512-tuple of numbers, of the form <code>(<var>atime</var>, <var>mtime</var>)</code>
752which is used to set the access and modified times, respectively.
753Whether a directory can be given for <var>path</var> depends on whether the
754operating system implements directories as files (for example, Windows
755does not). Note that the exact times you set here may not be returned
756by a subsequent <tt class="function">stat()</tt> call, depending on the resolution
757with which your operating system records access and modification times;
758see <tt class="function">stat()</tt>.
759
760<span class="versionnote">Changed in version 2.0:
761Added support for <code>None</code> for <var>times</var>.</span>
762
763Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
764</dl>
765
766<P>
767<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
768 <td><nobr><b><tt id='l2h-1638' xml:id='l2h-1638' class="function">walk</tt></b>(</nobr></td>
769 <td><var>top</var><big>[</big><var>, topdown<code>=True</code>
770 </var><big>[</big><var>, onerror<code>=None</code></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
771<dd>
772<a id='l2h-1646' xml:id='l2h-1646'></a>
773<tt class="function">walk()</tt> generates the file names in a directory tree, by
774walking the tree either top down or bottom up.
775For each directory in the tree rooted at directory <var>top</var> (including
776<var>top</var> itself), it yields a 3-tuple
777<code>(<var>dirpath</var>, <var>dirnames</var>, <var>filenames</var>)</code>.
778
779<P>
780<var>dirpath</var> is a string, the path to the directory. <var>dirnames</var> is
781a list of the names of the subdirectories in <var>dirpath</var>
782(excluding <code>'.'</code> and <code>'..'</code>). <var>filenames</var> is a list of
783the names of the non-directory files in <var>dirpath</var>. Note that the
784names in the lists contain no path components. To get a full
785path (which begins with <var>top</var>) to a file or directory in
786<var>dirpath</var>, do <code>os.path.join(<var>dirpath</var>, <var>name</var>)</code>.
787
788<P>
789If optional argument <var>topdown</var> is true or not specified, the triple
790for a directory is generated before the triples for any of its
791subdirectories (directories are generated top down). If <var>topdown</var> is
792false, the triple for a directory is generated after the triples for all
793of its subdirectories (directories are generated bottom up).
794
795<P>
796When <var>topdown</var> is true, the caller can modify the <var>dirnames</var> list
797in-place (perhaps using <tt class="keyword">del</tt> or slice assignment), and
798<tt class="function">walk()</tt> will only recurse into the subdirectories whose names
799remain in <var>dirnames</var>; this can be used to prune the search,
800impose a specific order of visiting, or even to inform <tt class="function">walk()</tt>
801about directories the caller creates or renames before it resumes
802<tt class="function">walk()</tt> again. Modifying <var>dirnames</var> when <var>topdown</var> is
803false is ineffective, because in bottom-up mode the directories in
804<var>dirnames</var> are generated before <var>dirnames</var> itself is generated.
805
806<P>
807By default errors from the <code>os.listdir()</code> call are ignored. If
808optional argument <var>onerror</var> is specified, it should be a function;
809it will be called with one argument, an os.error instance. It can
810report the error to continue with the walk, or raise the exception
811to abort the walk. Note that the filename is available as the
812<code>filename</code> attribute of the exception object.
813
814<P>
815<div class="note"><b class="label">Note:</b>
816If you pass a relative pathname, don't change the current working
817directory between resumptions of <tt class="function">walk()</tt>. <tt class="function">walk()</tt>
818never changes the current directory, and assumes that its caller
819doesn't either.
820</div>
821
822<P>
823<div class="note"><b class="label">Note:</b>
824On systems that support symbolic links, links to subdirectories appear
825in <var>dirnames</var> lists, but <tt class="function">walk()</tt> will not visit them
826(infinite loops are hard to avoid when following symbolic links).
827To visit linked directories, you can identify them with
828<code>os.path.islink(<var>path</var>)</code>, and invoke <code>walk(<var>path</var>)</code>
829on each directly.
830</div>
831
832<P>
833This example displays the number of bytes taken by non-directory files
834in each directory under the starting directory, except that it doesn't
835look under any CVS subdirectory:
836
837<P>
838<div class="verbatim"><pre>
839import os
840from os.path import join, getsize
841for root, dirs, files in os.walk('python/Lib/email'):
842 print root, "consumes",
843 print sum(getsize(join(root, name)) for name in files),
844 print "bytes in", len(files), "non-directory files"
845 if 'CVS' in dirs:
846 dirs.remove('CVS') # don't visit CVS directories
847</pre></div>
848
849<P>
850In the next example, walking the tree bottom up is essential:
851<tt class="function">rmdir()</tt> doesn't allow deleting a directory before the
852directory is empty:
853
854<P>
855<div class="verbatim"><pre>
856# Delete everything reachable from the directory named in 'top',
857# assuming there are no symbolic links.
858# CAUTION: This is dangerous! For example, if top == '/', it
859# could delete all your disk files.
860import os
861for root, dirs, files in os.walk(top, topdown=False):
862 for name in files:
863 os.remove(os.path.join(root, name))
864 for name in dirs:
865 os.rmdir(os.path.join(root, name))
866</pre></div>
867
868<P>
869
870<span class="versionnote">New in version 2.3.</span>
871
872</dl>
873
874<P>
875
876<DIV CLASS="navigation">
877<div class='online-navigation'>
878<p></p><hr />
879<table align="center" width="100%" cellpadding="0" cellspacing="2">
880<tr>
881<td class='online-navigation'><a rel="prev" title="6.1.3 File Descriptor Operations"
882 href="os-fd-ops.html"><img src='../icons/previous.png'
883 border='0' height='32' alt='Previous Page' width='32' /></A></td>
884<td class='online-navigation'><a rel="parent" title="6.1 os "
885 href="module-os.html"><img src='../icons/up.png'
886 border='0' height='32' alt='Up One Level' width='32' /></A></td>
887<td class='online-navigation'><a rel="next" title="6.1.5 Process Management"
888 href="os-process.html"><img src='../icons/next.png'
889 border='0' height='32' alt='Next Page' width='32' /></A></td>
890<td align="center" width="100%">Python Library Reference</td>
891<td class='online-navigation'><a rel="contents" title="Table of Contents"
892 href="contents.html"><img src='../icons/contents.png'
893 border='0' height='32' alt='Contents' width='32' /></A></td>
894<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
895 border='0' height='32' alt='Module Index' width='32' /></a></td>
896<td class='online-navigation'><a rel="index" title="Index"
897 href="genindex.html"><img src='../icons/index.png'
898 border='0' height='32' alt='Index' width='32' /></A></td>
899</tr></table>
900<div class='online-navigation'>
901<b class="navlabel">Previous:</b>
902<a class="sectref" rel="prev" href="os-fd-ops.html">6.1.3 File Descriptor Operations</A>
903<b class="navlabel">Up:</b>
904<a class="sectref" rel="parent" href="module-os.html">6.1 os </A>
905<b class="navlabel">Next:</b>
906<a class="sectref" rel="next" href="os-process.html">6.1.5 Process Management</A>
907</div>
908</div>
909<hr />
910<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
911</DIV>
912<!--End of Navigation Panel-->
913<ADDRESS>
914See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
915</ADDRESS>
916</BODY>
917</HTML>