开发者

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().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜