File Write Attempt Silently Fails - Permissions Problem
I am attemp开发者_开发百科ting to write a small portion of text to an (as yet) non-existing file to an directory owned by the root user and group. I have had a look at the Python docs regarding checking if a user is authorised to write files - the code below reflects the doc example.
Here's the offending bit of code:
try:
out = open('/owned/by/root/somefile.txt', 'w')
except IOError as e:
if e.errno == errno.EACCESS:
print('Cannot write file due to permission error')
raise
else:
out.write('Some text content here')
out.close()
Upon running this code (even as root user), no error is printed to the terminal and no exception is raised; yet the file is never actually written to the directory.
EACCESS should be EACCES (derived from the standard unix C libs.) That wouldn't cause you not to see an error however as IOERROR would get raised and you're re-raising even if the if block doesn't execute. This sounds like a linux specific issue. Probably SELinux.
BTW a context manager would be useful to avoid the specific else close().
精彩评论