1
votes

This is my python script which creates a "test.txt" file when executed. when I execute using terminal (Ubuntu 11.04) root@gml-VirtualBox:/var/www/HMS# python test.py. It creates the "test.txt" in the /var/www/HMS directory as I expected.

test.py:

#!/usr/bin/env python

def init():
    filename = "test.txt"
    file = open(filename, 'w')
    file.write("This is the new content of test.txt :-)")
    file.close()
    print "done"

def main():
    init()

if __name__ == '__main__':
    main()

But when I try to call this test.py using PHP, It's not creating the 'test.txt' output file.

index.php:

$tmp = exec("python test.py");
echo "temp: $tmp";

Both test.py & index.php are in the same directory(/var/www/HMS/). But when I modified the test.py like this:

#!/usr/bin/env python

def init():
    print "done"

def main():
    init()

if __name__ == '__main__':
    main()

It prints temp: done in the browser, which is what I expected.

I don't know why my previous python code didn't work as expected.

1

1 Answers

1
votes

When you test the python script on the command line, you're running as root. Chances are you're not running the webserver as root (which is a good thing), and the webserver's user does not have appropriate permissions to create and/or write to that file.