1

I have a problem running another file from php. I want my php params to be the output of running a python file that calls another file itself.

Here is my php file:

<?php
    if (isset($_POST['submit'])) {
    $params = solve();
}

function solve() {
  exec("python array.py", $output);
  return $output;
}
?>

If array.py is simply:

if __name__ == "__main__":
    print 1
    print 2
    print 3
    print 4

I will get 1,2,3,4 for my output, but I as soon as I change array.py to the following file that calls os.system, I don't get anything. So the new array.py is:

import os

def main():
    os.system("python test.py") #test.py creates tmp.txt with 4 lines w/ values 1,2,3,4


def output():
    f = open("tmp.txt", "r")
    myReturn = []
    currentline = f.readline()

    while currentline:
          val = currentline[:-1]  #Getting rid of '\n'
          val = int(val)
          myReturn = myReturn + [val]
          currentline = f.readline()
    f.close()
    return myReturn


if __name__ == "__main__":
     main()
     o = output()
     print o[0]
     print o[1]
     print o[2]
     print o[3]

Also if I just run test.py, the output is the file tmp.txt:

 1
 2
 3
 4

So now, when I run my php file, the output tmp.txt is not even created in the directory and as a result I don't get any output from my php either. I am not sure why this is happening because when I just run array.py myself, I get the desired output, and the tmp file is created.

EDIT: I forgot to include: import os above.

1 Answer 1

4

Change exec to:

exec("python array.py 2>&1", $output)

Or check the web server or php error log. This will return the error output from the python script to your php script (not normally what you want in production).

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

7 Comments

oh I had the import os sorry. You mean "2>&1" will return the error to my php right? So I get this broken error when running my php from loading it: Traceback (most recent call last): File f = open(IOError: [Errno 13] Permission denied: 'tmp.txt').
Permission denied is because PHP running as one user (likely apache), and the folder is owned by another user (presumably you). You will need to grant apache write access to the folder.
Well the thing is it doesn't even create that tmp.txt, so that is why it can't open it from test.py
oh just saw your comment. How would I do that? and doesn't it have permission just because the file currently doesn't exist? So If I give the apache write access to the folder, then it will be able to create tmp.txt, right?
Secondary question (you really should start marking answers as accepted - or you will stop getting quality responses). stackoverflow.com/questions/7950470/…
|

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.