2015-12-02 74 views
-1

我試圖解決與我的代碼的問題,在那裏我執行我的函數play(),但我傳入--b的參數是int,我做錯了什麼?類型'int'的參數在我的函數中是不可迭代的

import argparse 
from num2words import num2words 
import subprocess 

def play(): 
    parser = argparse.ArgumentParser() 
parser.add_argument("--b", default='99',type=str,help="B?") 
    args = parser.parse_args() 
    for iteration in reversed(range(args.b)): 
     print('Wee!') 

play() 

if __name__ == '__main__': 
    subprocess.call(['./file.py', '10'], shell=True) 

我通過執行此:

>>> import sys; import subprocess; 
>>> subprocess.call([sys.executable, 'file.py', '--b', 10]) 
Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File ".\subprocess.py", line 480, in call 
    File ".\subprocess.py", line 633, in __init__ 
    File ".\subprocess.py", line 801, in _execute_child 
    File ".\subprocess.py", line 541, in list2cmdline 
TypeError: argument of type 'int' is not iterable 
+3

把'10'放在引號中:'「10」' – baldr

+0

我想你必須把它作爲字符串傳遞。所以@baldr說,嘗試'subprocess.call([sys.executable,'file.py',' - b',str(10)])' –

+0

@Memnon - 這給我一個錯誤:「TypeError: 'list'對象不可調用「 – Jshee

回答

5
subprocess.call([sys.executable, 'file.py', '--b', 10]) 

在參數列表中的所有參數subprocess.call(或模塊的其他功能)必須是字符串。所以,如果你改變10是一個字符串'10'相反,它會工作得很好:

subprocess.call([sys.executable, 'file.py', '--b', '10']) 

注意,當調用的文件無法執行調用使用subprocess Python文件不會給你任何異常。這是一個完全獨立的過程,如果它失敗了,只會產生一些錯誤輸出,然後你可以從子過程讀取。

+0

這是返回0,任何想法爲什麼? – Jshee

+0

@ user700070因爲['subprocess.call'](https://docs.python.org/3/library/subprocess.html#subprocess.call)返回被調用應用程序的錯誤代碼。 – poke

+0

爲什麼不輸出打印值? – Jshee

相關問題