调用python脚本时出错

调用python脚本时出错

本文介绍了使用exec()调用python脚本时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用一个简单的python脚本

I am trying to call a simple python script

#!/usr/local/python25/bin/python
print "hello world"

来自以下php脚本

 <?php
    echo exec("/usr/local/python25/bin/python myfile.py");
    ?>

但是什么也没发生.请告诉我这里有什么问题吗? (我还检查了其他线程,但无法解决问题)

But nothing was happened.Please tell me what is wrong here? (I also checked other thread but I could not solve my problem)

已解决的问题:我忘记授予访问/usr/local/python25/bin/python的权限.完成此操作后,问题解决了.非常感谢你的帮助!

Question Solved:I forgot to give the permission to access /usr/local/python25/bin/python. After I did this, the problem solved.Thank you so much for your help!

推荐答案

1.exec函数仅返回命令结果的最后一行.
2. python(python 3除外)中的print语句自动在末尾添加换行符.

1.The exec function just return the last line from the result of the command.
2. The print statement in python (except python 3) automatically adds a newline at the end.

这就是您觉得什么都没发生的原因.

This is the reason you feel nothing was happened.

您可以通过这种方式捕获整个输出.

You can catch the whole output by this way.

exec("/usr/local/python25/bin/python myfile.py 2>&1", $output);
print_r($output);

这篇关于使用exec()调用python脚本时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:05