Jshee
Jshee

Reputation: 2686

argument of type 'int' is not iterable in my function

Im trying to resolve an issue with my code, where I am executing my function play() , but the argument i pass in of --b is an int, what am I doing wrong?

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)

I am executing this via:

>>> 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

Upvotes: 1

Views: 3471

Answers (1)

poke
poke

Reputation: 387607

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

All arguments in the argument list to subprocess.call (or other functions of the module) need to be string. So if you change the 10 to be a string '10' instead, it will work just fine:

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

Note that calling a Python file using subprocess will not give you any exception when the called file fails to execute. It’s a completely separate process which—if it fails—just produces some error output you can read from the subprocess then.

Upvotes: 9

Related Questions