[英]Running python script from php and write to a file
我的php代码在htdocs / series / index.php中。 我的python代码位置是/home/shivam/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脚本以读取模式打开它时,它可以工作,但是当我尝试使用上述代码编写时,我无法这样做。 还有一件事,“以open(“ / home / shivam / Desktop / solution.txt”,“ w”)作为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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.