By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

permission

P: n/a
Is it possible to check if you have permission to access and or change a
directory or file?
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

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
Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
Skip Montanaro <sk**@pobox.com> writes:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.