python不编写来自php的if调用

2024-09-25 16:30:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本writetest.py,包含以下内容:

print "mark1"
with open("/testfile.txt", "a") as myfile:
    myfile.write("Hello World")
print "mark2"

当我在shell中调用scrip时,它会工作并在testfile.txt中添加“Hello World”。当我使用php exec命令调用它时,它不会写:

exec('python writetest.py', $output, $return_var);
print_r($output);
print_r($return_var);

打印内容:

Array ( [0] => mark1 )
1

我的python脚本运行,但在打开代码时停止


Tags: pytxt脚本helloworldoutputreturnvar
2条回答

我认为你的代码中有报价问题

exec('python writetest.py")

一旦你修好了,再试一次。 如果还是不行,我建议

exec('python writetest.py', $output , $return_var) 

见:http://php.net/manual/en/function.exec.php

然后打印输出并通过print\u r返回\u var,以便调试

print_r($output);
print_r($return_var);

正如艾恩伯指出的,这是一个权限问题。我给了web服务器用户写访问权限,它成功了

相关问题 更多 >