I am trying to save git fetch output to file through python, using:
subprocess.check_output(["git", "fetch", "origin", ">>", "C:/bitbucket_backup/backup.log", "2>&1"], cwd='C:/bitbucket_backup/loopx')
but I believe there is something missing in subprocess.check_output args because when adding >> C:/bitbucket_backup/backup.log 2>&1 I receive this error:
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
subprocess.check_output(["git", "fetch", "origin", ">>", "C://bitbucket_backup//backup.log", "2>&1"], cwd='C://bitbucket_backup//loopx')
File "C:\Users\fabio\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\fabio\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['git', 'fetch', 'origin', '>>', 'C://bitbucket_backup//backup.log', '2>&1']' returned non-zero exit status 128.
subprocess.check_outputwill automatically pipe your STDOUT, and if you're expecting an error usesubprocess.communicate()instead to capture both streams. You can usesubprocess.Popen()to fully control the piping, including automatic piping to a file handle if you want to save the output as a file.shell=True(but that's dirty :))git fetch's output: it's not designed for machine parsing. Moreover it behaves differently if stdout is a pipe vs a tty. (There are some flags to try to control this but I ran into a bug trying to use them to show progress in parallel fetching.)