开发者

Are my permissions set correctly? (python)

In python I'm doing a os.system('chmod o+w filename.png') command so I can overwrite the 开发者_如何学Cfile with pngcrush.

These are the permissions after I set them in python:

-rw-rw-rw- 1 me users 925 Sep 20 11:25 filename.png

Then I attempt:

os.system('pngcrush filename.png filename.png')

which is supposed to overwrite the file, but I get:

Cannot overwrite input file filename.png

What could be the problem? Isn't pngcrush being run as an 'other' user, for which write permissions are enabled?

Thanks!


The problem is with the way you execute the pngcrush program, not with permissions of filename.png or Python. It simply attempts to open filename.png both for input and output, which is of course invalid.

Give pngcrush either the -e or the -d option to tell it how to write output. Read its man for more information.


Perhaps pngcrush isn't letting you use the same name for both input and output files? Have you tried changing the output filename? If so, what were the results?


As an aside (not related to the problem of the input and output files being the same), you can change the mode of a file using os.chmod, which is more efficient than running chmod:

import os
import stat

path = "filename.png"
mode = os.stat(path).st_mode     # get current mode
newmode = mode | stat.S_IWOTH    # set the 'others can write' bit
os.chmod(path, newmode)          # set new mode


Perhaps you're supposed to give a different (non-existing) filename for output. Have you tried the same in a shell?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜