2

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
Jimmy
  • 35
  • 5
  • When you use a `subprocess` you shouldn't use the `os.system` command, which returns an integer. You should just run the command as it is. – toti08 Sep 12 '18 at 08:15

1 Answers1

1

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]
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • when i print the output i just get b' ' there is nothing even inside the quotes. – Jimmy Sep 12 '18 at 08:17
  • There may have been an error from your `you-get` command. You should check what the error message says in the second item of the tuple returned by the `communicate` method (change `[0]` to `[1]`). – blhsing Sep 12 '18 at 08:21
  • That is correct some crashed other worked but i get None for both that worked and crashed now. – Jimmy Sep 12 '18 at 08:23
  • And for the one that work i did get not understandable output, i tried to decode using print(output).decode('utf-8') but it didn't work. – Jimmy Sep 12 '18 at 08:27