Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / os-file-dir.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="os-process.html" />
<link rel="prev" href="os-fd-ops.html" />
<link rel="parent" href="module-os.html" />
<link rel="next" href="os-process.html" />
<meta name='aesop' content='information' />
<title>6.1.4 Files and Directories </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.1.3 File Descriptor Operations"
href="os-fd-ops.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6.1 os "
href="module-os.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.1.5 Process Management"
href="os-process.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="os-fd-ops.html">6.1.3 File Descriptor Operations</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-os.html">6.1 os </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="os-process.html">6.1.5 Process Management</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION008140000000000000000"></A><A NAME="os-file-dir"></A>
<BR>
6.1.4 Files and Directories
</H2>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1598' xml:id='l2h-1598' class="function">access</tt></b>(</nobr></td>
<td><var>path, mode</var>)</td></tr></table></dt>
<dd>
Use the real uid/gid to test for access to <var>path</var>. Note that most
operations will use the effective uid/gid, therefore this routine can
be used in a suid/sgid environment to test if the invoking user has the
specified access to <var>path</var>. <var>mode</var> should be <tt class="constant">F_OK</tt>
to test the existence of <var>path</var>, or it can be the inclusive OR of
one or more of <tt class="constant">R_OK</tt>, <tt class="constant">W_OK</tt>, and <tt class="constant">X_OK</tt> to
test permissions. Return <tt class="constant">True</tt> if access is allowed,
<tt class="constant">False</tt> if not.
See the <span class="Unix">Unix</span> man page <span class="manpage"><i>access</i>(2)</span> for more information.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
<P>
<span class="note"><b class="label">Note:</b>
Using <tt class="function">access()</tt> to check if a user is authorized to e.g.
open a file before actually doing so using <tt class="function">open()</tt> creates a
security hole, because the user might exploit the short time interval
between checking and opening the file to manipulate it.</span>
</dl>
<P>
<dl><dt><b><tt id='l2h-1599' xml:id='l2h-1599'>F_OK</tt></b></dt>
<dd>
Value to pass as the <var>mode</var> parameter of <tt class="function">access()</tt> to
test the existence of <var>path</var>.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-1600' xml:id='l2h-1600'>R_OK</tt></b></dt>
<dd>
Value to include in the <var>mode</var> parameter of <tt class="function">access()</tt>
to test the readability of <var>path</var>.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-1601' xml:id='l2h-1601'>W_OK</tt></b></dt>
<dd>
Value to include in the <var>mode</var> parameter of <tt class="function">access()</tt>
to test the writability of <var>path</var>.
</dd></dl>
<P>
<dl><dt><b><tt id='l2h-1602' xml:id='l2h-1602'>X_OK</tt></b></dt>
<dd>
Value to include in the <var>mode</var> parameter of <tt class="function">access()</tt>
to determine if <var>path</var> can be executed.
</dd></dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1603' xml:id='l2h-1603' class="function">chdir</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
<a id='l2h-1639' xml:id='l2h-1639'></a>
Change the current working directory to <var>path</var>.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1604' xml:id='l2h-1604' class="function">fchdir</tt></b>(</nobr></td>
<td><var>fd</var>)</td></tr></table></dt>
<dd>
Change the current working directory to the directory represented by
the file descriptor <var>fd</var>. The descriptor must refer to an opened
directory, not an open file.
Availability: <span class="Unix">Unix</span>.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1605' xml:id='l2h-1605' class="function">getcwd</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return a string representing the current working directory.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1606' xml:id='l2h-1606' class="function">getcwdu</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return a Unicode object representing the current working directory.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1607' xml:id='l2h-1607' class="function">chroot</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Change the root directory of the current process to <var>path</var>.
Availability: Macintosh, <span class="Unix">Unix</span>.
<span class="versionnote">New in version 2.2.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1608' xml:id='l2h-1608' class="function">chmod</tt></b>(</nobr></td>
<td><var>path, mode</var>)</td></tr></table></dt>
<dd>
Change the mode of <var>path</var> to the numeric <var>mode</var>.
<var>mode</var> may take one of the following values
(as defined in the <tt class="module">stat</tt> module):
<UL>
<LI><code>S_ISUID</code>
</LI>
<LI><code>S_ISGID</code>
</LI>
<LI><code>S_ENFMT</code>
</LI>
<LI><code>S_ISVTX</code>
</LI>
<LI><code>S_IREAD</code>
</LI>
<LI><code>S_IWRITE</code>
</LI>
<LI><code>S_IEXEC</code>
</LI>
<LI><code>S_IRWXU</code>
</LI>
<LI><code>S_IRUSR</code>
</LI>
<LI><code>S_IWUSR</code>
</LI>
<LI><code>S_IXUSR</code>
</LI>
<LI><code>S_IRWXG</code>
</LI>
<LI><code>S_IRGRP</code>
</LI>
<LI><code>S_IWGRP</code>
</LI>
<LI><code>S_IXGRP</code>
</LI>
<LI><code>S_IRWXO</code>
</LI>
<LI><code>S_IROTH</code>
</LI>
<LI><code>S_IWOTH</code>
</LI>
<LI><code>S_IXOTH</code>
</LI>
</UL>
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
<P>
<span class="note"><b class="label">Note:</b>
Although Windows supports <tt class="function">chmod()</tt>, you can only
set the file's read-only flag with it (via the <code>S_IWRITE</code>
and <code>S_IREAD</code> constants or a corresponding integer value).
All other bits are ignored.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1609' xml:id='l2h-1609' class="function">chown</tt></b>(</nobr></td>
<td><var>path, uid, gid</var>)</td></tr></table></dt>
<dd>
Change the owner and group id of <var>path</var> to the numeric <var>uid</var>
and <var>gid</var>. To leave one of the ids unchanged, set it to -1.
Availability: Macintosh, <span class="Unix">Unix</span>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1610' xml:id='l2h-1610' class="function">lchown</tt></b>(</nobr></td>
<td><var>path, uid, gid</var>)</td></tr></table></dt>
<dd>
Change the owner and group id of <var>path</var> to the numeric <var>uid</var>
and gid. This function will not follow symbolic links.
Availability: Macintosh, <span class="Unix">Unix</span>.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1611' xml:id='l2h-1611' class="function">link</tt></b>(</nobr></td>
<td><var>src, dst</var>)</td></tr></table></dt>
<dd>
Create a hard link pointing to <var>src</var> named <var>dst</var>.
Availability: Macintosh, <span class="Unix">Unix</span>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1612' xml:id='l2h-1612' class="function">listdir</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Return a list containing the names of the entries in the directory.
The list is in arbitrary order. It does not include the special
entries <code>'.'</code> and <code>'..'</code> even if they are present in the
directory.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
<P>
<span class="versionnote">Changed in version 2.3:
On Windows NT/2k/XP and Unix, if <var>path</var> is a Unicode
object, the result will be a list of Unicode objects..</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1613' xml:id='l2h-1613' class="function">lstat</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Like <tt class="function">stat()</tt>, but do not follow symbolic links.
Availability: Macintosh, <span class="Unix">Unix</span>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1614' xml:id='l2h-1614' class="function">mkfifo</tt></b>(</nobr></td>
<td><var>path</var><big>[</big><var>, mode</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Create a FIFO (a named pipe) named <var>path</var> with numeric mode
<var>mode</var>. The default <var>mode</var> is <code>0666</code> (octal). The current
umask value is first masked out from the mode.
Availability: Macintosh, <span class="Unix">Unix</span>.
<P>
FIFOs are pipes that can be accessed like regular files. FIFOs exist
until they are deleted (for example with <tt class="function">os.unlink()</tt>).
Generally, FIFOs are used as rendezvous between ``client'' and
``server'' type processes: the server opens the FIFO for reading, and
the client opens it for writing. Note that <tt class="function">mkfifo()</tt>
doesn't open the FIFO -- it just creates the rendezvous point.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1615' xml:id='l2h-1615' class="function">mknod</tt></b>(</nobr></td>
<td><var>path</var><big>[</big><var>, mode=0600, device</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Create a filesystem node (file, device special file or named pipe)
named filename. <var>mode</var> specifies both the permissions to use and
the type of node to be created, being combined (bitwise OR) with one
of S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO (those constants are
available in <tt class="module">stat</tt>). For S_IFCHR and S_IFBLK, <var>device</var>
defines the newly created device special file (probably using
<tt class="function">os.makedev()</tt>), otherwise it is ignored.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1616' xml:id='l2h-1616' class="function">major</tt></b>(</nobr></td>
<td><var>device</var>)</td></tr></table></dt>
<dd>
Extracts a device major number from a raw device number.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1617' xml:id='l2h-1617' class="function">minor</tt></b>(</nobr></td>
<td><var>device</var>)</td></tr></table></dt>
<dd>
Extracts a device minor number from a raw device number.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1618' xml:id='l2h-1618' class="function">makedev</tt></b>(</nobr></td>
<td><var>major, minor</var>)</td></tr></table></dt>
<dd>
Composes a raw device number from the major and minor device numbers.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1619' xml:id='l2h-1619' class="function">mkdir</tt></b>(</nobr></td>
<td><var>path</var><big>[</big><var>, mode</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Create a directory named <var>path</var> with numeric mode <var>mode</var>.
The default <var>mode</var> is <code>0777</code> (octal). On some systems,
<var>mode</var> is ignored. Where it is used, the current umask value is
first masked out.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1620' xml:id='l2h-1620' class="function">makedirs</tt></b>(</nobr></td>
<td><var>path</var><big>[</big><var>, mode</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Recursive directory creation function.<a id='l2h-1640' xml:id='l2h-1640'></a>
Like <tt class="function">mkdir()</tt>,
but makes all intermediate-level directories needed to contain the
leaf directory. Throws an <tt class="exception">error</tt> exception if the leaf
directory already exists or cannot be created. The default <var>mode</var>
is <code>0777</code> (octal). This function does not properly handle UNC
paths (only relevant on Windows systems; Universal Naming Convention
paths are those that use the `<code>&#92;&#92;host&#92;path</code>' syntax).
<span class="versionnote">New in version 1.5.2.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1621' xml:id='l2h-1621' class="function">pathconf</tt></b>(</nobr></td>
<td><var>path, name</var>)</td></tr></table></dt>
<dd>
Return system configuration information relevant to a named file.
<var>name</var> specifies the configuration value to retrieve; it may be a
string which is the name of a defined system value; these names are
specified in a number of standards (POSIX.1, <span class="Unix">Unix</span> 95, <span class="Unix">Unix</span> 98, and
others). Some platforms define additional names as well. The names
known to the host operating system are given in the
<code>pathconf_names</code> dictionary. For configuration variables not
included in that mapping, passing an integer for <var>name</var> is also
accepted.
Availability: Macintosh, <span class="Unix">Unix</span>.
<P>
If <var>name</var> is a string and is not known, <tt class="exception">ValueError</tt> is
raised. If a specific value for <var>name</var> is not supported by the
host system, even if it is included in <code>pathconf_names</code>, an
<tt class="exception">OSError</tt> is raised with <tt class="constant">errno.EINVAL</tt> for the
error number.
</dl>
<P>
<dl><dt><b><tt id='l2h-1622' xml:id='l2h-1622'>pathconf_names</tt></b></dt>
<dd>
Dictionary mapping names accepted by <tt class="function">pathconf()</tt> and
<tt class="function">fpathconf()</tt> to the integer values defined for those names
by the host operating system. This can be used to determine the set
of names known to the system.
Availability: Macintosh, <span class="Unix">Unix</span>.
</dd></dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1623' xml:id='l2h-1623' class="function">readlink</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Return a string representing the path to which the symbolic link
points. The result may be either an absolute or relative pathname; if
it is relative, it may be converted to an absolute pathname using
<code>os.path.join(os.path.dirname(<var>path</var>), <var>result</var>)</code>.
Availability: Macintosh, <span class="Unix">Unix</span>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1624' xml:id='l2h-1624' class="function">remove</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Remove the file <var>path</var>. If <var>path</var> is a directory,
<tt class="exception">OSError</tt> is raised; see <tt class="function">rmdir()</tt> below to remove
a directory. This is identical to the <tt class="function">unlink()</tt> function
documented below. On Windows, attempting to remove a file that is in
use causes an exception to be raised; on <span class="Unix">Unix</span>, the directory entry is
removed but the storage allocated to the file is not made available
until the original file is no longer in use.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1625' xml:id='l2h-1625' class="function">removedirs</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
<a id='l2h-1641' xml:id='l2h-1641'></a>
Removes directories recursively. Works like
<tt class="function">rmdir()</tt> except that, if the leaf directory is
successfully removed, directories corresponding to rightmost path
segments will be pruned way until either the whole path is consumed or
an error is raised (which is ignored, because it generally means that
a parent directory is not empty). Throws an <tt class="exception">error</tt>
exception if the leaf directory could not be successfully removed.
<span class="versionnote">New in version 1.5.2.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1626' xml:id='l2h-1626' class="function">rename</tt></b>(</nobr></td>
<td><var>src, dst</var>)</td></tr></table></dt>
<dd>
Rename the file or directory <var>src</var> to <var>dst</var>. If <var>dst</var> is
a directory, <tt class="exception">OSError</tt> will be raised. On <span class="Unix">Unix</span>, if
<var>dst</var> exists and is a file, it will be removed silently if the
user has permission. The operation may fail on some <span class="Unix">Unix</span> flavors
if <var>src</var> and <var>dst</var> are on different filesystems. If
successful, the renaming will be an atomic operation (this is a
POSIX requirement). On Windows, if <var>dst</var> already exists,
<tt class="exception">OSError</tt> will be raised even if it is a file; there may be
no way to implement an atomic rename when <var>dst</var> names an existing
file.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1627' xml:id='l2h-1627' class="function">renames</tt></b>(</nobr></td>
<td><var>old, new</var>)</td></tr></table></dt>
<dd>
Recursive directory or file renaming function.
Works like <tt class="function">rename()</tt>, except creation of any intermediate
directories needed to make the new pathname good is attempted first.
After the rename, directories corresponding to rightmost path segments
of the old name will be pruned away using <tt class="function">removedirs()</tt>.
<span class="versionnote">New in version 1.5.2.</span>
<P>
<div class="note"><b class="label">Note:</b>
This function can fail with the new directory structure made if
you lack permissions needed to remove the leaf directory or file.
</div>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1628' xml:id='l2h-1628' class="function">rmdir</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Remove the directory <var>path</var>.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1629' xml:id='l2h-1629' class="function">stat</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Perform a <tt class="cfunction">stat()</tt> system call on the given path. The
return value is an object whose attributes correspond to the members of
the <tt class="ctype">stat</tt> structure, namely:
<tt class="member">st_mode</tt> (protection bits),
<tt class="member">st_ino</tt> (inode number),
<tt class="member">st_dev</tt> (device),
<tt class="member">st_nlink</tt> (number of hard links),
<tt class="member">st_uid</tt> (user ID of owner),
<tt class="member">st_gid</tt> (group ID of owner),
<tt class="member">st_size</tt> (size of file, in bytes),
<tt class="member">st_atime</tt> (time of most recent access),
<tt class="member">st_mtime</tt> (time of most recent content modification),
<tt class="member">st_ctime</tt>
(platform dependent; time of most recent metadata change on <span class="Unix">Unix</span>, or
the time of creation on Windows).
<P>
<span class="versionnote">Changed in version 2.3:
If <tt class="function">stat_float_times</tt> returns true, the time
values are floats, measuring seconds. Fractions of a second may be
reported if the system supports that. On Mac OS, the times are always
floats. See <tt class="function">stat_float_times</tt> for further discussion. .</span>
<P>
On some Unix systems (such as Linux), the following attributes may
also be available:
<tt class="member">st_blocks</tt> (number of blocks allocated for file),
<tt class="member">st_blksize</tt> (filesystem blocksize),
<tt class="member">st_rdev</tt> (type of device if an inode device).
<P>
On Mac OS systems, the following attributes may also be available:
<tt class="member">st_rsize</tt>,
<tt class="member">st_creator</tt>,
<tt class="member">st_type</tt>.
<P>
On RISCOS systems, the following attributes are also available:
<tt class="member">st_ftype</tt> (file type),
<tt class="member">st_attrs</tt> (attributes),
<tt class="member">st_obtype</tt> (object type).
<P>
For backward compatibility, the return value of <tt class="function">stat()</tt> is
also accessible as a tuple of at least 10 integers giving the most
important (and portable) members of the <tt class="ctype">stat</tt> structure, in the
order
<tt class="member">st_mode</tt>,
<tt class="member">st_ino</tt>,
<tt class="member">st_dev</tt>,
<tt class="member">st_nlink</tt>,
<tt class="member">st_uid</tt>,
<tt class="member">st_gid</tt>,
<tt class="member">st_size</tt>,
<tt class="member">st_atime</tt>,
<tt class="member">st_mtime</tt>,
<tt class="member">st_ctime</tt>.
More items may be added at the end by some implementations.
The standard module <tt class="module"><a href="module-stat.html">stat</a></tt><a id='l2h-1642' xml:id='l2h-1642'></a> defines
functions and constants that are useful for extracting information
from a <tt class="ctype">stat</tt> structure.
(On Windows, some items are filled with dummy values.)
<P>
<span class="note"><b class="label">Note:</b>
The exact meaning and resolution of the <tt class="member">st_atime</tt>,
<tt class="member">st_mtime</tt>, and <tt class="member">st_ctime</tt> members depends on the
operating system and the file system. For example, on Windows systems
using the FAT or FAT32 file systems, <tt class="member">st_mtime</tt> has 2-second
resolution, and <tt class="member">st_atime</tt> has only 1-day resolution. See
your operating system documentation for details.</span>
<P>
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
<P>
<span class="versionnote">Changed in version 2.2:
Added access to values as attributes of the returned object.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1630' xml:id='l2h-1630' class="function">stat_float_times</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>newvalue</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Determine whether <tt class="class">stat_result</tt> represents time stamps as float
objects. If newval is True, future calls to stat() return floats, if
it is False, future calls return ints. If newval is omitted, return
the current setting.
<P>
For compatibility with older Python versions, accessing
<tt class="class">stat_result</tt> as a tuple always returns integers. For
compatibility with Python 2.2, accessing the time stamps by field name
also returns integers. Applications that want to determine the
fractions of a second in a time stamp can use this function to have
time stamps represented as floats. Whether they will actually observe
non-zero fractions depends on the system.
<P>
Future Python releases will change the default of this setting;
applications that cannot deal with floating point time stamps can then
use this function to turn the feature off.
<P>
It is recommended that this setting is only changed at program startup
time in the <var>__main__</var> module; libraries should never change this
setting. If an application uses a library that works incorrectly if
floating point time stamps are processed, this application should turn
the feature off until the library has been corrected.
<P>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1631' xml:id='l2h-1631' class="function">statvfs</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Perform a <tt class="cfunction">statvfs()</tt> system call on the given path. The
return value is an object whose attributes describe the filesystem on
the given path, and correspond to the members of the
<tt class="ctype">statvfs</tt> structure, namely:
<tt class="member">f_frsize</tt>,
<tt class="member">f_blocks</tt>,
<tt class="member">f_bfree</tt>,
<tt class="member">f_bavail</tt>,
<tt class="member">f_files</tt>,
<tt class="member">f_ffree</tt>,
<tt class="member">f_favail</tt>,
<tt class="member">f_flag</tt>,
<tt class="member">f_namemax</tt>.
Availability: <span class="Unix">Unix</span>.
<P>
For backward compatibility, the return value is also accessible as a
tuple whose values correspond to the attributes, in the order given above.
The standard module <tt class="module"><a href="module-statvfs.html">statvfs</a></tt><a id='l2h-1643' xml:id='l2h-1643'></a>
defines constants that are useful for extracting information
from a <tt class="ctype">statvfs</tt> structure when accessing it as a sequence; this
remains useful when writing code that needs to work with versions of
Python that don't support accessing the fields as attributes.
<P>
<span class="versionnote">Changed in version 2.2:
Added access to values as attributes of the returned object.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1632' xml:id='l2h-1632' class="function">symlink</tt></b>(</nobr></td>
<td><var>src, dst</var>)</td></tr></table></dt>
<dd>
Create a symbolic link pointing to <var>src</var> named <var>dst</var>.
Availability: <span class="Unix">Unix</span>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1633' xml:id='l2h-1633' class="function">tempnam</tt></b>(</nobr></td>
<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>
<dd>
Return a unique path name that is reasonable for creating a temporary
file. This will be an absolute path that names a potential directory
entry in the directory <var>dir</var> or a common location for temporary
files if <var>dir</var> is omitted or <code>None</code>. If given and not
<code>None</code>, <var>prefix</var> is used to provide a short prefix to the
filename. Applications are responsible for properly creating and
managing files created using paths returned by <tt class="function">tempnam()</tt>;
no automatic cleanup is provided.
On <span class="Unix">Unix</span>, the environment variable <a class="envvar" id='l2h-1644' xml:id='l2h-1644'>TMPDIR</a> overrides
<var>dir</var>, while on Windows the <a class="envvar" id='l2h-1645' xml:id='l2h-1645'>TMP</a> is used. The specific
behavior of this function depends on the C library implementation;
some aspects are underspecified in system documentation.
<span class="warning"><b class="label">Warning:</b>
Use of <tt class="function">tempnam()</tt> is vulnerable to symlink attacks;
consider using <tt class="function">tmpfile()</tt> (section <A href="os-newstreams.html#os-newstreams">6.1.2</A>)
instead.</span> Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1634' xml:id='l2h-1634' class="function">tmpnam</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return a unique path name that is reasonable for creating a temporary
file. This will be an absolute path that names a potential directory
entry in a common location for temporary files. Applications are
responsible for properly creating and managing files created using
paths returned by <tt class="function">tmpnam()</tt>; no automatic cleanup is
provided.
<span class="warning"><b class="label">Warning:</b>
Use of <tt class="function">tmpnam()</tt> is vulnerable to symlink attacks;
consider using <tt class="function">tmpfile()</tt> (section <A href="os-newstreams.html#os-newstreams">6.1.2</A>)
instead.</span> Availability: <span class="Unix">Unix</span>, Windows. This function probably
shouldn't be used on Windows, though: Microsoft's implementation of
<tt class="function">tmpnam()</tt> always creates a name in the root directory of the
current drive, and that's generally a poor location for a temp file
(depending on privileges, you may not even be able to open a file
using this name).
</dl>
<P>
<dl><dt><b><tt id='l2h-1635' xml:id='l2h-1635'>TMP_MAX</tt></b></dt>
<dd>
The maximum number of unique names that <tt class="function">tmpnam()</tt> will
generate before reusing names.
</dd></dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1636' xml:id='l2h-1636' class="function">unlink</tt></b>(</nobr></td>
<td><var>path</var>)</td></tr></table></dt>
<dd>
Remove the file <var>path</var>. This is the same function as
<tt class="function">remove()</tt>; the <tt class="function">unlink()</tt> name is its traditional
<span class="Unix">Unix</span> name.
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1637' xml:id='l2h-1637' class="function">utime</tt></b>(</nobr></td>
<td><var>path, times</var>)</td></tr></table></dt>
<dd>
Set the access and modified times of the file specified by <var>path</var>.
If <var>times</var> is <code>None</code>, then the file's access and modified
times are set to the current time. Otherwise, <var>times</var> must be a
2-tuple of numbers, of the form <code>(<var>atime</var>, <var>mtime</var>)</code>
which is used to set the access and modified times, respectively.
Whether a directory can be given for <var>path</var> depends on whether the
operating system implements directories as files (for example, Windows
does not). Note that the exact times you set here may not be returned
by a subsequent <tt class="function">stat()</tt> call, depending on the resolution
with which your operating system records access and modification times;
see <tt class="function">stat()</tt>.
<span class="versionnote">Changed in version 2.0:
Added support for <code>None</code> for <var>times</var>.</span>
Availability: Macintosh, <span class="Unix">Unix</span>, Windows.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1638' xml:id='l2h-1638' class="function">walk</tt></b>(</nobr></td>
<td><var>top</var><big>[</big><var>, topdown<code>=True</code>
</var><big>[</big><var>, onerror<code>=None</code></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
<a id='l2h-1646' xml:id='l2h-1646'></a>
<tt class="function">walk()</tt> generates the file names in a directory tree, by
walking the tree either top down or bottom up.
For each directory in the tree rooted at directory <var>top</var> (including
<var>top</var> itself), it yields a 3-tuple
<code>(<var>dirpath</var>, <var>dirnames</var>, <var>filenames</var>)</code>.
<P>
<var>dirpath</var> is a string, the path to the directory. <var>dirnames</var> is
a list of the names of the subdirectories in <var>dirpath</var>
(excluding <code>'.'</code> and <code>'..'</code>). <var>filenames</var> is a list of
the names of the non-directory files in <var>dirpath</var>. Note that the
names in the lists contain no path components. To get a full
path (which begins with <var>top</var>) to a file or directory in
<var>dirpath</var>, do <code>os.path.join(<var>dirpath</var>, <var>name</var>)</code>.
<P>
If optional argument <var>topdown</var> is true or not specified, the triple
for a directory is generated before the triples for any of its
subdirectories (directories are generated top down). If <var>topdown</var> is
false, the triple for a directory is generated after the triples for all
of its subdirectories (directories are generated bottom up).
<P>
When <var>topdown</var> is true, the caller can modify the <var>dirnames</var> list
in-place (perhaps using <tt class="keyword">del</tt> or slice assignment), and
<tt class="function">walk()</tt> will only recurse into the subdirectories whose names
remain in <var>dirnames</var>; this can be used to prune the search,
impose a specific order of visiting, or even to inform <tt class="function">walk()</tt>
about directories the caller creates or renames before it resumes
<tt class="function">walk()</tt> again. Modifying <var>dirnames</var> when <var>topdown</var> is
false is ineffective, because in bottom-up mode the directories in
<var>dirnames</var> are generated before <var>dirnames</var> itself is generated.
<P>
By default errors from the <code>os.listdir()</code> call are ignored. If
optional argument <var>onerror</var> is specified, it should be a function;
it will be called with one argument, an os.error instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
<code>filename</code> attribute of the exception object.
<P>
<div class="note"><b class="label">Note:</b>
If you pass a relative pathname, don't change the current working
directory between resumptions of <tt class="function">walk()</tt>. <tt class="function">walk()</tt>
never changes the current directory, and assumes that its caller
doesn't either.
</div>
<P>
<div class="note"><b class="label">Note:</b>
On systems that support symbolic links, links to subdirectories appear
in <var>dirnames</var> lists, but <tt class="function">walk()</tt> will not visit them
(infinite loops are hard to avoid when following symbolic links).
To visit linked directories, you can identify them with
<code>os.path.islink(<var>path</var>)</code>, and invoke <code>walk(<var>path</var>)</code>
on each directly.
</div>
<P>
This example displays the number of bytes taken by non-directory files
in each directory under the starting directory, except that it doesn't
look under any CVS subdirectory:
<P>
<div class="verbatim"><pre>
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print root, "consumes",
print sum(getsize(join(root, name)) for name in files),
print "bytes in", len(files), "non-directory files"
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
</pre></div>
<P>
In the next example, walking the tree bottom up is essential:
<tt class="function">rmdir()</tt> doesn't allow deleting a directory before the
directory is empty:
<P>
<div class="verbatim"><pre>
# Delete everything reachable from the directory named in 'top',
# assuming there are no symbolic links.
# CAUTION: This is dangerous! For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
</pre></div>
<P>
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.1.3 File Descriptor Operations"
href="os-fd-ops.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6.1 os "
href="module-os.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.1.5 Process Management"
href="os-process.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="os-fd-ops.html">6.1.3 File Descriptor Operations</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-os.html">6.1 os </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="os-process.html">6.1.5 Process Management</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>