1

I am trying

git status *> test.txt 

This works in powershell well but if I do this using python -

os.system('git status'+' *> '+log_path+'\git_output.txt')

fatal: '*' does not appear to be a git repository fatal: Could not read from remote repository.

os.system('git status'+' 2> '+log_path+'\git_output.txt')

Btw, this worked! But I want all kinds of output in the file!

What is the best way to do this?

torek
  • 448,244
  • 59
  • 642
  • 775
Aditya Sinha
  • 614
  • 8
  • 18
  • Powershell might interpolate `*`. – dan1st May 31 '21 at 20:31
  • 1
    Note that Git itself is somewhat unusual in that it often writes "normal" output to its standard error output (file descriptor 2 in C code, `os.stderr` in Python). As [mklelement0 notes in a comment](https://stackoverflow.com/questions/67779860/github-commands-output-in-a-file-using-python-in-windows-10#comment119805131_67779992), the issue here is that PowerShell is *not being used* by `os.system`. Consider using `subprocess.Popen` to capture both stdout and stderr. – torek Jun 01 '21 at 00:36
  • @dan1st But is there any option to use *> forcefully specifying the exact intent for it? – Aditya Sinha Jun 01 '21 at 06:28
  • You can explicitely run the command in `powershell`. – dan1st Jun 01 '21 at 06:29
  • @torek So does Popen work for all git commands? My intent is to create an automated git syncup between two repository. In order to do so I have to parse all errors and outputs – Aditya Sinha Jun 01 '21 at 06:30
  • @dan1st But it won't solve my problem statement then – Aditya Sinha Jun 01 '21 at 06:31
  • @AdityaSinha: it *can*, depending on what you mean by "work" and "all". :-) More seriously: Git *can* run unattended (as it does when invoked by, say, `go get`). Git *can* run attended (as it does when invoked by a user). A Git session that is intended to interact with a user may need a *controlling terminal*, such as provided by a pty: see [when to use pty.fork()](https://stackoverflow.com/questions/1922254/python-when-to-use-pty-fork-versus-os-fork). – torek Jun 01 '21 at 06:36
  • If you intended your outer Python process to pretend to be a user, doing pretend-interactions with a Git command, you may need to do all of that. If you intend your outer Python process to be simple and clean and *not* pretend to be a user, don't do any of that, and do use `subprocess.Popen` or some wrapper for it. – torek Jun 01 '21 at 06:37
  • 1
    Also related, with more links for much more background reading: https://stackoverflow.com/q/34186035/1256452 – torek Jun 01 '21 at 06:42

2 Answers2

3

Have a look at redirection from command prompt (different from powershell)

os.system('git status'+' 1> '+log_path+'\git_output.txt 2>&1')
Daniel
  • 4,792
  • 2
  • 7
  • 20
1

This implementation captures the results and writes them to a file:

output_file = open('git_output.txt', 'w')
output_file.write(os.popen('git status').read())
output_file.close()

Although it doesn't capture errors (stderr output).

Joep
  • 788
  • 2
  • 8
  • 23
  • 2
    The OP's only problem is a redirection (`>`) _syntax_ problem: `os.system()` calls `cmd.exe` as the default shell, which doesn't understand PowerShell-only `*>` redirections. With `cmd.exe`-appropriate redirection syntax, `os.system()` works just fine in this case. Your code doesn't also capture _stderr_, the inclusion of which is the OP's intent. – mklement0 May 31 '21 at 22:11
  • @AdityaSinha, no, this approach doesn't capture errors (stderr output). Joep, I'm glad you value my comment, but please consider amending your answer directly. – mklement0 Jun 01 '21 at 11:55