I have an installation script written in Python (in Linux) that runs as root and needs to check whether certain files are readable by a non-root user.
For this reason I can’t use os.path.exists() or open(filename) (and catch any exceptions).
Currently I’m thinking of checking the permission bit on each of the files, but the only problem is that I will have to check the permission bits on the path leading up to the filename as well (directories need r+x bits set), which could be very slow process if I have thousands of files.
Is my solution the best one, or are there better alternatives?
edit: I will need the script run as root after the files are checked, so dropping root permissions is not an option unfortunately.
You could use os.seteuid to change the effective user to some non-root user. Then try opening the file. An
IOErrorwill be raised if permission is denied.