繁体   English   中英

python脚本中的“ no such file or directory”没有在Debian Linux中调用其他Python脚本

[英]“no such file or directory” in python script calling other python script in debian linux

嗨,我在/ home / user / a /目录下的linux debian中用python脚本pythonscript1.py编写了以下代码:

import subprocess
subprocess.call(["python" /home/user/b/phpcall.py"])

phpcall.py脚本包含:

import subprocess
subprocess.call(["php", "/home/user/b/phpscript1.php"])

从控制台单独调用所有脚本都可以正常运行,但是当我使用第一个脚本而第二个脚本调用/查找目录b而不是目录a中的文件时,会产生以下错误:

"PHP warning: include_once(something.php): failed to open stream: no such file in /home/user/b/phpschript1.php on line 25

现在对我来说很清楚,问题是它无法到达其原始目录。 但是我不知道我应该在第一个脚本中添加什么命令,以允许第二个脚本在文件夹b中查找。

到目前为止,谷歌搜索结果建议使用“ include_path ='include'”,但我不知道如何/在何处成功合并该语句。

关于正确语法的任何建议将不胜感激!

为了将来参考,直接从python中调用php脚本的代码是:

import os
os.chdir("/home/user/b")
os.system("php /home/user/b/phpscript1.php")

感谢Marc B&我喜欢塞巴斯蒂安的动态方法,谢谢您:)

您可以尝试以下程序:

import subprocess
import sys
proc = subprocess.Popen(
       [sys.executable, '/home/user/b/phpcall.py'],stdin=subprocess.PIPE)

proc.communicate()

我认为这是工作。 如果不告诉。

如果php脚本仅在使用python脚本从目录启动时才有效,则可以使用cwd参数更改子cwd的工作目录:

#!/usr/bin/env python
from subprocess import check_call

check_call(["php", "/home/user/b/phpscript1.php"], cwd="/home/user/b")

我添加了shebang #!/usr/bin/env python以便您可以假定脚本是可执行文件( chmod +x phpcall.py )直接作为/path/to/phpcall.py运行该脚本。

注意:如果要以user user身份运行python脚本,则可以使用~指定路径:

#!/usr/bin/env python
import os
from subprocess import check_call

script_dir = os.path.expanduser("~/b")
check_call(["php", os.path.join(script_dir, "phpscript1.php")], cwd=script_dir)

为了避免对脚本目录进行硬编码,可以动态找到它:

#!/usr/bin/env python
import os
from subprocess import check_call

script_dir = get_script_dir()
check_call(["php", os.path.join(script_dir, "phpscript1.php")], cwd=script_dir)

其中get_script_dir()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM