os/stat broken under 2.2 / Linux ?
Fredrik Lundh
fredrik at pythonware.com
Tue Nov 12 18:27:51 EST 2002
Pasiphe's Bull wrote:
> Python 2.2 (#1, Apr 12 2002, 15:29:57)
> [GCC 2.96 20000731 (Red Hat Linux 7.2 2.96-109)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import stat
> >>> import os
> >>> file_mode = os.stat("./foo")
> >>> if (stat.S_ISDIR(file_mode)):
> ... print "is dir"
> ... else:
> ... print "is NOT dir"
> ...
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "/usr/lib/python2.2/stat.py", line 46, in S_ISDIR
> return S_IFMT(mode) == S_IFDIR
> File "/usr/lib/python2.2/stat.py", line 30, in S_IFMT
> return mode & 0170000
> TypeError: unsupported operand type(s) for &: 'posix.stat_result' and 'int'
> >>>
os.stat returns a tuple containing the mode and lots of other
stuff, S_ISDIR expects a mode integer and nothing else.
try:
file_mode = os.stat("./foo")[stat.ST_MODE]
if stat.S_ISDIR(file_mode):
...
or (in 2.2 and later, which returns a tuple with attributes)
file_mode = os.stat("./foo").st_mode
if stat.S_ISDIR(file_mode):
...
or, easier and more pythonic:
if os.path.isdir("./foo"):
...
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list