views:

46

answers:

4

Hello,

In python I'm doing a os.system('chmod o+w filename.png') command so I can overwrite the file 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!

+2  A: 

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?

jathanism
+3  A: 

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.

Eli Bendersky
A: 

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

knitti
+1  A: 

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
Richard Fearn
Very cool, thanks!
Jasie