Jimmy
Jimmy

Reputation: 35

How can i retrieve the output shown on my command line using python?

Using ubuntu 16.04 what i want is to retrieve the output on terminal using python, i did refer to these 2 links : How can I get terminal output in python?

Running shell command from Python and capturing the output

but being a starter in python i couldn't make it work, my original code which output i want:

for element in my_images:
    os.system('you-get -o videos ' + element)

how my code became :

for element in my_images:
    # value = os.system('you-get -o videos ' + element)
    output = subprocess.Popen(os.system('you-get -o videos ' + element), stdout=subprocess.PIPE).communicate()[0]
    print(output)

but it didn't work i get this error

Traceback (most recent call last):
  File "main_twitter.py", line 29, in <module>
    output = subprocess.Popen(os.system('you-get -o videos ' + element), stdout=subprocess.PIPE).communicate()[0]
  File "/usr/local/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/usr/local/lib/python3.6/subprocess.py", line 1220, in _execute_child
    args = list(args)
TypeError: 'int' object is not iterable

Upvotes: 2

Views: 286

Answers (1)

blhsing
blhsing

Reputation: 107115

You should call the constructor of Popen with the command and arguments directly. Calling os.system would execute the command and return the exit code, which is not what you want to pass to Popen:

output = subprocess.Popen('you-get -o videos ' + element, shell=True, stdout=subprocess.PIPE).communicate()[0]

Upvotes: 1

Related Questions