-1

我可以通过在命令行上执行 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
4

1 回答 1

0

通过在php中执行一个bat文件来运行python脚本解决了这个问题。

exec('cmd /c ./path/run_example.bat')

这是我写的bat文件:

cd C:\Users\Username\AppData\Local\Programs\Python\Python36 # very important if you are runnning it on a server.
python --version # make sure the version is the same as what you expected
python C:\path\example.py
于 2017-07-03T09:38:20.187 回答