类型'int'的参数在我的函数中是不可迭代的

Jsh*_*hee -1 python python-3.x

我试图解决我的代码问题,我正在执行我的函数play(),但我传入的参数--b是一个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)
Run Code Online (Sandbox Code Playgroud)

我通过以下方式执行:

>>> 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
Run Code Online (Sandbox Code Playgroud)

pok*_*oke 8

subprocess.call([sys.executable, 'file.py', '--b', 10])
Run Code Online (Sandbox Code Playgroud)

参数列表中的所有参数subprocess.call(或模块的其他函数)都需要是字符串.因此,如果您将其更改10为字符串'10',它将正常工作:

subprocess.call([sys.executable, 'file.py', '--b', '10'])
Run Code Online (Sandbox Code Playgroud)

请注意,调用Python文件subprocess时,如果被调用文件无法执行,则不会发生任何异常.这是一个完全独立的过程 - 如果它失败 - 只是产生一些错误输出,你可以从子进程读取.