I have a pile of python scripts that I am trying to use within IronPython (I need C# functionality), and I am having some issues with the os.access method.
When using the os.access method with the mode set to os.F_OK (see whether a path can be accessed), I am getting False returned every time, even when the python interpreter returns True for that directory. The strange thing about this though, is that the os.access method called with the os.R_OK, os.W_OK and os.X_OK modes returns True.
Here is a code sample I knocked up to demonstrate the issue:
import os
d = ["C:\\", ".\\", "./", ".", "C:\\BOGUS_DIR"]
for path in d:
print path
try:
print "access: %s"%os.access(path, os.F_OK)
print "read: %s"%os.access(path, os.R_OK)
print "write: %s"%os.access(path, os.W_OK)
print "execute: %s"%os.access(path, os.X_OK)
print "-------------------------"
except Exception, e:
print e
print "finished"
raw_input("")
The results:
C:\
access: False
read: True
write: True
execute: True
-------------------------
.\
access: False
read: True
write: True
execute: True
-------------------------
./
access: False
read: True
write: True
execute: True
------开发者_高级运维-------------------
.
access: False
read: True
write: True
execute: True
-------------------------
C:\BOGUS_DIR
access: False
[Errno 2] Could not find file 'C:\BOGUS_DIR'.
finished
For good measure, I added a bogus directory to the list (a directory that doesn't exist). The os.access with mode os.F_OK returned False (as always), but the call to check os.R_OK throws an exception (as expected).
Also, I tested the os.access method in the python interpreter, which worked fine.
I can't seem to find anybody else with this issue, which is why my first assumption is that I've done something silly (or missed something obvious). I am running the IronPython code through Visual Studio 2010, perhaps there is something interfering there.
Any help would be greatly appreciated. Thanks
For one, os.access can be misleading - it's better to just open the file and catch any exceptions that occur.
IronPython uses os.F_OK to mean "does this file exist", and boils it down to a call to File.Exists()
- which returns false for a directory.
This is a bug, and should be fixed in 2.7.1.
精彩评论