How to use os.access()??

Peter Hansen peter at engcorp.com
Sat Jul 14 00:52:20 EDT 2001


bvdpoel at uniserve.com wrote:
> 
> I'm trying to use os.access() in my program. But, I'm stuck trying to
> figure out the parms... obviously the first is the filename; but the 2nd
> is supposed to be a constant (?) like F_OK. Unfortunately, it doesn't
> appear that the F_OK, etc are defined anywhere. Either the docs are
> wrong (no way!), or I'm missing a module file (likely), or I'm just
> totally wrong about everything (quite likely!).

You're right! (that you're totally wrong... ;-)

>>> import os
>>> dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_RDONLY', 
'O_RDWR', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 
'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'TMP_MAX', 'UserDict', 
'W_OK', 'X_OK', ... ]
>>> os.access('c:/autoexec.bat', os.F_OK)
1
>>> F_OK
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'F_OK' is not defined
>>> os.F_OK
0
>>> from os import *
>>> F_OK
0
>>>

You've probably forgotten to use os.F_OK and are using just F_OK.

Unless you want to "from os import *", which is generally bad form,
you need to prepend the name of the module in front of _everything_
you use from that module, whether function name or constant.

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list