Catch bash/git fatal in python
Question: I would like to write a simple python script which will be able to clone a git
repository into desired directory. I used try...except construction to be able
to catch all exceptions however it looks like I am not able to handle 'fatal'
properly.

    
    
    #!/usr/bin/env python
    import subprocess
    
    try:
        subprocess.check_call(['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory'])
    except Exception:
        print "There was a problem during repository configuration"
    

The output of the script above:

> fatal: repository 'git clone git@some_repo' does not exist
>
> There was a problem during repository configuration

To be more specific, I was rather expecting to get only the "There was a ..."
message. Why do I get a 'fatal' message also?


Answer: The message you are seeing is produced by the `git` command.

If you want to prevent that message from appearing you should redirect either
standard error or all output to `/dev/null` through a shell, like:

    
    
    subprocess.check_call(['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory', '2&>/dev/null'], shell=True)
    

However, I'd recommend against that practice since you lose information on the
actual cause of error.

