控制子进程命令的环境(controlling environment for subprocess command)

编程入门 行业动态 更新时间:2024-10-25 12:24:30
控制子进程命令的环境(controlling environment for subprocess command)

我正在尝试使用特定的绝对路径启动脚本

from subprocess import check_output

如果我运行:

# myscript.py check_command=['pwd'] ret=check_output(check_command,shell=True) print(ret) path_set_command=['cd MYABSPATH'] ret=check_output(path_set_command,shell=True) ret=check_output(check_command,shell=True) print(ret)

它打印两次myscript.py所在的文件夹。 因此,我明白每次调用check_output重新创建shell中的环境变量。

如何使用check_ouput设置命令的路径? 我试图让我的命令为

ret=check_output(['cd MYABSPATH; ./otherscript.py'] ,shell=True)

但是,如果我将参数传递给我的otherscript.py (将它们作为额外项目附加到包含该命令的列表中),则它们无法正确转发。

那么如何用foo bar参数在MYABSPATH运行MYABSPATH ?

I am trying to start a script from a specific absolute path using

from subprocess import check_output

If I run:

# myscript.py check_command=['pwd'] ret=check_output(check_command,shell=True) print(ret) path_set_command=['cd MYABSPATH'] ret=check_output(path_set_command,shell=True) ret=check_output(check_command,shell=True) print(ret)

it prints two times the folder where myscript.py is located. Hence, I understand that the environment variables in the shell are recreated each call to check_output.

How to set the path of a command with check_ouput? I have tried to have my command as

ret=check_output(['cd MYABSPATH; ./otherscript.py'] ,shell=True)

however, if I pass arguments to my otherscript.py (appending them as extra item to the list containing the command) they are not correctly forwarded.

So how do I run otherscript.py in MYABSPATH with foo bar arguments?

最满意答案

你可以使用cwd:

foo = check_output("pwd", cwd="/MYABSPATH", shell=True)

You could use cwd:

foo = check_output("pwd", cwd="/MYABSPATH", shell=True)

更多推荐

本文发布于:2023-07-27 05:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1285501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   命令   环境   controlling   subprocess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!