Python3. Prompt requires ctrl+c and halts script
Question: I'm writing a small script that runs through a directory and attempts to
obtain version numbers by running "filename --version". Now for the most part
it works or fails in a manageable way. However I've come across one file
"iptables-restore" in /sbin/ that fails when --version is passed to it.
However the failure leaves the prompt in a state that requires a ctrl+z or
ctrl+c to return to the prompt, and thus halts my script.

Here's the code:

    
    
    try:            
            subOut = subprocess.check_output([fname, "--version"])
        except subprocess.CalledProcessError as cpE:                                                                                                   
            fObj.write('{0:25}RETURN CODE:{1:15}\t\t{2:30}\n'.format(fnamecolon, cpE.returncode, cpE.output))
            fnamecolon = ''
            pass
        except Exception as e:
            pass
    

I just wondered if there's an elegant way to handle this - either via a return
code or perhaps an Exception.

I'm fairly new to Python and am doing this for practice really - so any
guidance is greatly appreciated.

Thanks all!


Answer: So this works better - probably still some things I'm not understanding
though...

    
    
    try:
            # devnull=open(os.devnull, 'w')
            try:
                from subprocess import DEVNULL # Python 3
            except ImportError:
                DEVNULL = open(os.devnull, 'r+b', 0)
            subOut = Popen([fname, "--version"], stdin=DEVNULL, stdout=PIPE, stderr=STDOUT, close_fds=True)
            output = subOut.stdout.read()
        except subprocess.CalledProcessError as cpE:                                                                                                   
            fObj.write('{0:25}RETURN CODE:{1:15}\t\t{2:30}\n'.format(fnamecolon, cpE.returncode, cpE.output))
            fname = ''
            pass
        except Exception as e:
            # fObj.write('{0:30}{1:30}{2:30}\n'.format(fname, e.returncode, e.output))
            pass     
    

