How do I print the output of the exec() function in python 3.5?
Question: How do I have it so that you pass in a python command to the exec() command,
waits for completion, and print out the output of everything that just
happened?

Many of the code out there uses StringIO, something that is not included in
Python 3.5.


Answer: You can't. [Exec just executes in place and returns
nothing](https://docs.python.org/3/library/functions.html#exec). Your best bet
would be to write the command into a script and execute it with
[subprocess](https://docs.python.org/3/library/subprocess.html) if you really
want to catch all the output.

Here's an example for you:

    
    
    #!/usr/bin/env python3
    
    from sys import argv, executable
    from tempfile import NamedTemporaryFile
    from subprocess import check_output
    
    with NamedTemporaryFile(mode='w') as file:
        file.write('\n'.join(argv[1:]))
        file.write('\n')
        file.flush()
    
        output = check_output([executable, file.name])
    
        print('output from command: {}'.format(output))
    

And running it:

    
    
    $ ./catchandrun.py 'print("hello world!")' 
    output from command: b'hello world!\n'
    $
    

