1

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks alot for your answer Mat..Now I fixed that. However I have similar problem when executing nltk.WordNetLemmatizer() function. This time permission is not an issue, as I checked that after your suggestion. If you are familiar with NLTK library, can he help me with this
How did you set permissions for webserver?
A lot of shared server providers won't allow to use exec(). So you're python script will never be called at all this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.