<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>6.3 stat --
Interpreting stat() results</TITLE>
<META NAME="description" CONTENT="6.3 stat --
Interpreting stat() results">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-time.html">
<LINK REL="previous" HREF="module-os.path.html">
<LINK REL="up" HREF="allos.html">
<LINK REL="next" HREF="module-time.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-time.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="allos.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-os.path.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-time.html">6.4 time  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="allos.html">6. Generic Operating System</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-os.path.html">6.2 os.path  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION008300000000000000000">
6.3 <tt class=module>stat</tt> --
         Interpreting <tt class=function>stat()</tt> results</A>
</H1>

<P>


<P>
The <tt class=module>stat</tt> module defines constants and functions for
interpreting the results of <tt class=function>os.stat()</tt>,
<tt class=function>os.fstat()</tt> and <tt class=function>os.lstat()</tt> (if they exist).  For
complete details about the <tt class=cfunction>stat()</tt>, <tt class=cfunction>fstat()</tt> and
<tt class=cfunction>lstat()</tt> calls, consult the documentation for your system.

<P>
The <tt class=module>stat</tt> module defines the following functions to test for
specific file types:

<P>
<dl><dt><b><a name="l2h-1014"><tt class=function>S_ISDIR</tt></a></b> (<var>mode</var>)
<dd>
Return non-zero if the mode is from a directory.
</dl>

<P>
<dl><dt><b><a name="l2h-1015"><tt class=function>S_ISCHR</tt></a></b> (<var>mode</var>)
<dd>
Return non-zero if the mode is from a character special device file.
</dl>

<P>
<dl><dt><b><a name="l2h-1016"><tt class=function>S_ISBLK</tt></a></b> (<var>mode</var>)
<dd>
Return non-zero if the mode is from a block special device file.
</dl>

<P>
<dl><dt><b><a name="l2h-1017"><tt class=function>S_ISREG</tt></a></b> (<var>mode</var>)
<dd>
Return non-zero if the mode is from a regular file.
</dl>

<P>
<dl><dt><b><a name="l2h-1018"><tt class=function>S_ISFIFO</tt></a></b> (<var>mode</var>)
<dd>
Return non-zero if the mode is from a FIFO (named pipe).
</dl>

<P>
<dl><dt><b><a name="l2h-1019"><tt class=function>S_ISLNK</tt></a></b> (<var>mode</var>)
<dd>
Return non-zero if the mode is from a symbolic link.
</dl>

<P>
<dl><dt><b><a name="l2h-1020"><tt class=function>S_ISSOCK</tt></a></b> (<var>mode</var>)
<dd>
Return non-zero if the mode is from a socket.
</dl>

<P>
Two additional functions are defined for more general manipulation of
the file's mode:

<P>
<dl><dt><b><a name="l2h-1021"><tt class=function>S_IMODE</tt></a></b> (<var>mode</var>)
<dd>
Return the portion of the file's mode that can be set by
<tt class=function>os.chmod()</tt>--that is, the file's permission bits, plus the
sticky bit, set-group-id, and set-user-id bits (on systems that support
them).
</dl>

<P>
<dl><dt><b><a name="l2h-1022"><tt class=function>S_IFMT</tt></a></b> (<var>mode</var>)
<dd>
Return the portion of the file's mode that describes the file type (used
by the <tt class=function>S_IS*()</tt> functions above).
</dl>

<P>
Normally, you would use the <tt class=function>os.path.is*()</tt> functions for
testing the type of a file; the functions here are useful when you are
doing multiple tests of the same file and wish to avoid the overhead of
the <tt class=cfunction>stat()</tt> system call for each test.  These are also
useful when checking for information about a file that isn't handled
by <tt class=module><a href="module-os.path.html">os.path</a></tt>, like the tests for block and character
devices.

<P>
All the variables below are simply symbolic indexes into the 10-tuple
returned by <tt class=function>os.stat()</tt>, <tt class=function>os.fstat()</tt> or
<tt class=function>os.lstat()</tt>.

<P>
<dl><dt><b><a name="l2h-1023"><tt>ST_MODE</tt></a></b>
<dd>
Inode protection mode.
</dl>

<P>
<dl><dt><b><a name="l2h-1024"><tt>ST_INO</tt></a></b>
<dd>
Inode number.
</dl>

<P>
<dl><dt><b><a name="l2h-1025"><tt>ST_DEV</tt></a></b>
<dd>
Device inode resides on.
</dl>

<P>
<dl><dt><b><a name="l2h-1026"><tt>ST_NLINK</tt></a></b>
<dd>
Number of links to the inode.
</dl>

<P>
<dl><dt><b><a name="l2h-1027"><tt>ST_UID</tt></a></b>
<dd>
User id of the owner.
</dl>

<P>
<dl><dt><b><a name="l2h-1028"><tt>ST_GID</tt></a></b>
<dd>
Group id of the owner.
</dl>

<P>
<dl><dt><b><a name="l2h-1029"><tt>ST_SIZE</tt></a></b>
<dd>
File size in bytes.
</dl>

<P>
<dl><dt><b><a name="l2h-1030"><tt>ST_ATIME</tt></a></b>
<dd>
Time of last access.
</dl>

<P>
<dl><dt><b><a name="l2h-1031"><tt>ST_MTIME</tt></a></b>
<dd>
Time of last modification.
</dl>

<P>
<dl><dt><b><a name="l2h-1032"><tt>ST_CTIME</tt></a></b>
<dd>
Time of last status change (see manual pages for details).
</dl>

<P>
Example:

<P>
<dl><dd><pre class=verbatim>
import os, sys
from stat import *

def process(dir, func):
    '''recursively descend the directory rooted at dir, calling func for
       each regular file'''

    for f in os.listdir(dir):
        mode = os.stat('%s/%s' % (dir, f))[ST_MODE]
if S_ISDIR(mode):
            # recurse into directory
            process('%s/%s' % (dir, f), func)
elif S_ISREG(mode):
            func('%s/%s' % (dir, f))
else:
            print 'Skipping %s/%s' % (dir, f)

def f(file):
    print 'frobbed', file

if __name__ == '__main__':
    process(sys.argv[1], f)
</pre></dl><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-time.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="allos.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-os.path.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-time.html">6.4 time  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="allos.html">6. Generic Operating System</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-os.path.html">6.2 os.path  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
