【发布时间】:2017-12-06 08:47:21
【问题描述】:
我可以通过在命令行上执行 python 文件来获得输出(在文本文件中),但无法通过使用 php exec() 执行来获得输出(在文本文件中)。我做错了什么?
这是我的python文件(example.py):
inputfile = open("input.txt",r)
data = inputfile.read()
inputfile.close()
outputfile = open("output.txt","w")
outputfile.write(str(data))
outputfile.write(str(data))
outputfile.close()
这是我的 php 文件(example2.php):
<?php
$string = "hello";
file_put_contents('input.txt', $string);
exec('python example.py');
$result = file_get_contents('output.txt');
我在命令行上输入了这个,它起作用了:
python example.py
【问题讨论】:
-
尝试使用绝对路径。
-
或许this answer 可以帮忙
-
使用
shell_exec().. 这里是答案:running-python-script-in-php-capture-all-outputs -
@KlausD。谢谢你的建议。它有部分帮助。将发布解决方案。下面。
标签: php python cmd exec cmdline-args