I'm hitting a block on what I thought would be a relatively simple task. I've written a script that searches an xhtml file searching for a particular tag in a particular line. if the tag is there - great, if not I want to modify the line of the file that needs to be updated. To accomplish this, i make a copy of the file with a "~" appended to the filename as a backup, using shutil.copy2(). Then I 'open' the original file name to write the copied file into line by line, changing the lines that need updating. Finally it deletes the file with the "~" after confirming the file changes have happened correctly. Everything worked great in my test environment, but when I downloaded a copy of our site repository, I was hit with errno 13 - no permissions to write. Is there a way I can adjust the mode on the fly to avoid this? I figured 10 minutes of searching would turn up a similar problem/solution, but i've been at it for a while now with no good idea for a workaround. Any constructive ideas pushing me in the right direction would be appreciated. If this has already been answered my apologies, please point me in the right direction, I couldn't find a similar question on here the way I've been searching. thanks folks.
+3
A:
If you have permission to change permissions, os.chmod will do that for you. Otherwise, you need to speak with the system administrators, or the owners of that directory, and get directory permissions changed appropriately. For example:
import os
import stats
os.chmod('/path/to/thefile', stat.S_IROTH|stats.S_IWOTH)
assuming it's a unix-y system and you are an "other" from the file's ownership's viewpoint (i.e., you are neither the user owning the file, nor in the group owning it). (You'll probably also want to add more of the various stat.S_...
flags to that or, to avoid removing writeability or readability from the file's owner and group).
Alex Martelli
2010-05-17 17:05:04
Thanks Alex, I knew there was an obvious tool I was blanking on.
jonny
2010-05-17 19:25:45