douyang5943 2016-03-31 19:00
浏览 46

从php运行python脚本并写入文件

My php code is in htdocs/series/index.php. My python code location is /home/shivam/Desktop/hello.py I am running php using xampp in ubuntu. here is php code

 <?php
$command = escapeshellcmd('/home/shivam/Desktop/hello.py');
$output = shell_exec($command);
echo $output;
?>

here is my python code

#!/usr/bin/env python
with open("/home/shivam/Desktop/solution.txt", "w") as myfile:
    myfile.write("write text")

I am trying to run the python script using php to write to a file 'solution.txt'. When i write something myself to solution.txt and open it in read mode using python script via php then it works but when i try to write using above code ,i am unable to do so. One more thing, none of the code below the line "with open("/home/shivam/Desktop/solution.txt", "w") as myfile:" gets executed. I have tried the solution given in : enter link description here

  • 写回答

1条回答 默认 最新

  • dqg17080 2016-04-01 06:09
    关注

    Absolute paths to executables and files...

    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")
    

    Output:

    user@box:~/$ php run_py.php 
    user@box:~/$ cat solution.txt 
    write text
    
    评论

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀