permission

J

James

Is it possible to check if you have permission to access and or change a
directory or file?
 
S

Skip Montanaro

James> Is it possible to check if you have permission to access and or
James> change a directory or file?

Yes, but it's generally much easier to try, then recover from any errors:

try:
f = open(somefile, "a")
except IOError, msg:
print "can't open", somefile, "for writing"
else:
f.write("hi mom!\n")

Skip
 
K

Kartic

James said the following on 4/12/2005 11:00 PM:
Is it possible to check if you have permission to access and or change a
directory or file?


James - You can use the access() in the os module.

import os

print os.access('C:\\', os.R_OK | os.W_OK) # check for read/write access
# hopefully print True
print os.access('C:\\noexist.file', os.R_OK) # Check no existent file
# will print False
print os.access('C:\\Docume~1\\anotheruser\\Mydocu~1', os.R_OK) # Read
another users documents folder
# should print False if no read access

Please read http://docs.python.org/lib/os-file-dir.html for more info.

Thanks,
-Kartic
 
M

Mike Meyer

Skip Montanaro said:
James> Is it possible to check if you have permission to access and or
James> change a directory or file?

Yes, but it's generally much easier to try, then recover from any errors:

Especially since the answer to the question may change between
checking and trying - so you have to be prepared to deal with failure
either way.

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,234
Messages
2,571,179
Members
47,811
Latest member
GregoryHal

Latest Threads

Top