簡體   English   中英

Subprocess.Popen()=>無輸出

[英]Subprocess.Popen() => No output

我試圖從Python中運行一個Perl腳本,但是在stdout()中沒有輸出,而當我從shell運行它時,我的腳本完全正常。

首先,這是我如何從shell執行它(假設我在正確的目錄中):

./vmlinkedclone.pl --server 192.168.20.2 --username root --password root 
--vmbase_id 2 --vm_destination_id 41 --vmname_destination "clone-41-snapname" --snapname Snapname

#=> True, []
#=> or False, and a description of the error here 
#=> or an argument error

以下是我嘗試從Python調用它的方法:

cmd = ['/home/user/workspace/vmlinkedclone.pl', '--server', '192.168.20.2', '--username', 'root', '--password', 'root' ,'--vmbase_id', '2', '--vm_destination_id', '41', '--vmname_destination', 'clone-41-snapname', '--snapname', 'Snapname']
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = pipe.stdout.read()

print "Result : ",result
#=> Result :

當我從Shell運行腳本時,為什么我得到所需的輸出,而從Python中什么也得不到?

你能嘗試一下:

pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

編輯

我確實發現了一些與編碼相關的問題,我用以下方式解決了這個問題:

import subprocess
cmd = ['/home/user/workspace/vmlinkedclone.pl', '--server', '192.168.20.2', '--username', 'root', '--password', 'root' ,'--vmbase_id', '2', '--vm_destination_id', '41', '--vmname_destination', 'clone-41-snapname', '--snapname', 'Snapname']
pipe = subprocess.Popen(cmd, shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = pipe.communicate()
result = out.decode()
print "Result : ",result 

暫無
暫無

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

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