<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<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=
"module-statcache.html" />
<link rel=
"prev" href=
"module-dircache.html" />
<link rel=
"parent" href=
"allos.html" />
<link rel=
"next" href=
"module-statcache.html" />
<meta name='aesop' content='information'
/>
<title>6.4 stat -- Interpreting stat() results
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.3 dircache "
href=
"module-dircache.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. Generic Operating System"
href=
"allos.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.5 statcache "
href=
"module-statcache.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>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-dircache.html">6.3 dircache
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"allos.html">6. Generic Operating System
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-statcache.html">6.5 statcache
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION008400000000000000000">
6.4 <tt class=
"module">stat
</tt> --
Interpreting
<tt class=
"function">stat()
</tt> results
</A>
<A NAME=
"module-stat"></A>
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.
The
<tt class=
"module">stat
</tt> module defines the following functions to test for
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1776' xml:id='l2h-
1776'
class=
"function">S_ISDIR
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return non-zero if the mode is from a directory.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1777' xml:id='l2h-
1777'
class=
"function">S_ISCHR
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return non-zero if the mode is from a character special device file.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1778' xml:id='l2h-
1778'
class=
"function">S_ISBLK
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return non-zero if the mode is from a block special device file.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1779' xml:id='l2h-
1779'
class=
"function">S_ISREG
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return non-zero if the mode is from a regular file.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1780' xml:id='l2h-
1780'
class=
"function">S_ISFIFO
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return non-zero if the mode is from a FIFO (named pipe).
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1781' xml:id='l2h-
1781'
class=
"function">S_ISLNK
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return non-zero if the mode is from a symbolic link.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1782' xml:id='l2h-
1782'
class=
"function">S_ISSOCK
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return non-zero if the mode is from a socket.
Two additional functions are defined for more general manipulation of
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1783' xml:id='l2h-
1783'
class=
"function">S_IMODE
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
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
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
1784' xml:id='l2h-
1784'
class=
"function">S_IFMT
</tt></b>(
</nobr></td>
<td><var>mode
</var>)
</td></tr></table></dt>
Return the portion of the file's mode that describes the file type (used
by the
<tt class=
"function">S_IS*()
</tt> functions above).
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
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>.
<dl><dt><b><tt id='l2h-
1785' xml:id='l2h-
1785'
>ST_MODE
</tt></b></dt>
<dl><dt><b><tt id='l2h-
1786' xml:id='l2h-
1786'
>ST_INO
</tt></b></dt>
<dl><dt><b><tt id='l2h-
1787' xml:id='l2h-
1787'
>ST_DEV
</tt></b></dt>
<dl><dt><b><tt id='l2h-
1788' xml:id='l2h-
1788'
>ST_NLINK
</tt></b></dt>
Number of links to the inode.
<dl><dt><b><tt id='l2h-
1789' xml:id='l2h-
1789'
>ST_UID
</tt></b></dt>
<dl><dt><b><tt id='l2h-
1790' xml:id='l2h-
1790'
>ST_GID
</tt></b></dt>
<dl><dt><b><tt id='l2h-
1791' xml:id='l2h-
1791'
>ST_SIZE
</tt></b></dt>
Size in bytes of a plain file; amount of data waiting on some special
<dl><dt><b><tt id='l2h-
1792' xml:id='l2h-
1792'
>ST_ATIME
</tt></b></dt>
<dl><dt><b><tt id='l2h-
1793' xml:id='l2h-
1793'
>ST_MTIME
</tt></b></dt>
Time of last modification.
<dl><dt><b><tt id='l2h-
1794' xml:id='l2h-
1794'
>ST_CTIME
</tt></b></dt>
The ``ctime'' as reported by the operating system. On some systems
(like
<span class=
"Unix">Unix
</span>) is the time of the last metadata change, and, on others
(like Windows), is the creation time (see platform documentation for
The interpretation of ``file size'' changes according to the file
type. For plain files this is the size of the file in bytes. For
FIFOs and sockets under most flavors of
<span class=
"Unix">Unix
</span> (including Linux in
particular), the ``size'' is the number of bytes waiting to be read at
the time of the call to
<tt class=
"function">os.stat()
</tt>,
<tt class=
"function">os.fstat()
</tt>,
or
<tt class=
"function">os.lstat()
</tt>; this can sometimes be useful, especially for
polling one of these special files after a non-blocking open. The
meaning of the size field for other character and block devices varies
more, depending on the implementation of the underlying system call.
<div class=
"verbatim"><pre>
def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file'''
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
# It's a directory, recurse into it
walktree(pathname, callback)
# It's a file, call the callback function
# Unknown file type, print a message
print 'Skipping %s' % pathname
if __name__ == '__main__':
walktree(sys.argv[
1], visitfile)
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.3 dircache "
href=
"module-dircache.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. Generic Operating System"
href=
"allos.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.5 statcache "
href=
"module-statcache.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>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-dircache.html">6.3 dircache
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"allos.html">6. Generic Operating System
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-statcache.html">6.5 statcache
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.