我的php代码在htdocs/sseries/index.php中。我的python代码位置是/home/sivam/Desktop/hello.py我在ubuntu中使用xampp运行php。这是php代码
<?php
$command = escapeshellcmd('/home/shivam/Desktop/hello.py');
$output = shell_exec($command);
echo $output;
?>
这是我的python代码
#!/usr/bin/env python
with open("/home/shivam/Desktop/solution.txt", "w") as myfile:
myfile.write("write text")
我正在尝试使用php运行python脚本,以写入文件"solution.txt"。当我自己将一些东西写入solution.txt,并通过php使用python脚本在读取模式下打开它时,它可以工作,但当我尝试使用上面的代码编写时,我无法做到这一点。还有一件事,"with open("/home/shivam/Desktop/solution.txt","w")as myfile:"行下面的代码都没有被执行。我已经尝试了中给出的解决方案:在此处输入链接描述
可执行文件和文件的绝对路径。。。
run_py.php
<?php
$command = escapeshellcmd('/usr/bin/python /home/user/some_py.py');
$output = shell_exec($command);
echo $output;
?>
some_py.py
with open("/home/user/solution.txt", "w") as myfile:
myfile.write("write text")
输出:
user@box:~/$ php run_py.php
user@box:~/$ cat solution.txt
write text