我正在編寫一個為用戶安裝命令行界面的腳本。proc = Popen("sudo -S apt-get install vim".split(), stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)# Popen only accepts byte-arrays so you must encode the stringoutput = proc.communicate(password.encode())stdoutput = (output)[0].decode('utf-8')輸出:Installing Vim...|Reading package lists...Building dependency tree...Reading state information...The following additional packages will be installed: vim-runtimeSuggested packages: ctags vim-doc vim-scriptsThe following NEW packages will be installed: vim vim-runtime0 upgraded, 2 newly installed, 0 to remove and 5 not upgraded.Need to get 7,111 kB of archives.After this operation, 34.6 MB of additional disk space will be used.Do you want to continue? [Y/n] Abort.如您所見,軟件包的安裝停止Do you want to continue?,并且由于某種原因,安裝被中止。我如何y在這個階段使用傳遞 a Popen()?我已經使用傳遞密碼communicate(),但沒有進行多次輸入。
1 回答

qq_遁去的一_1
TA貢獻1725條經驗 獲得超8個贊
-y只需作為選項傳遞apt-get即可避免與它(進一步)交互的需要。
proc = Popen(["sudo", "-S", "apt-get", "-y", "install", "vim"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, error = proc.communicate(password.encode())
stdoutput = output.decode('utf-8')
添加回答
舉報
0/150
提交
取消