簡體   English   中英

在Python子進程中打開Shell

[英]Open Shell within Python Subprocess

我有一個腳本,可以用不同於標准外殼的條件打開一個新外殼,我們稱它為./new_shell <arg> 我需要在Python腳本中運行./new_shell <arg> ,然后在該Shell中運行命令,然后退出該Shell並繼續照常執行我的腳本。 我怎樣才能做到這一點?

該目標(使用偽代碼)供參考:

for arg in args:
    subprocess.run(['./new_shell ' + arg], shell=True) #this doesn't behave as I want, because I need the shell to be automated, not manual
    subprocess.run([<various scripts that need to be run within new_shell])
    subprocess.run([<exit_shell>]) #don't know how to do this

非常感謝你!

我的解決方案是按如下方式使用Popen

for arg in args:
    pro = subprocess.Popen(['new_shell', arg],
                           stdin=subprocess.PIPE,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    inpt = <script_to_be_run_in_shell> + <arguments> +'\n'
    out, err = pro.communicate(inpt.encode())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM