开发者

difficulties with basic file i/o in python script called from php

Like the file says I am executing a python script from a PHP file sitting on my apache webserver. The example I have is extremely basic.

PHP file -- test.php

<?php
echo "hello";
echo exec("/usr/local/bin/python2.7 test1.py");
?>

python script -- test1.py

print "world"
fh = open ('testtest' ,'r')
for line in fh:开发者_运维百科
    print line

contents of -- testtest

abcdefg

'world' prints when I don't have the bottom 2 lines of test1.py but it stops printing as soon as I add for line in file: Thank you. Edit: I'm trying to print the contents of testtest.


exec does not do what you expect it to do. The documentation states that it only returns the last line of output. It recommends use of passthru instead.


You don't want to assign to the variable file. That's a built-in in Python. You should use something like fh for file handle, or inFile, or the like. That might be enough to address it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜