-3

I have the below, along with several f.write commands in my python, but I can't figure out how to get it to print/catch errors to the same file.

f=open("C:/path/to/file/log.text","a+")

Also, how would I do this for powershell? I tried the below, but it's not catching errors?

"Foo Bar Foo Bar" | Add-Content 'C:/path/to/file/log.text'

Thanks.

physlexic
  • 826
  • 2
  • 9
  • 21
  • Downvote for 'this question shows no research effort'. [How to redirect stderr in Python?](http://stackoverflow.com/q/1956142/478656) and [Temporarily Redirect stdout/stderr](http://stackoverflow.com/q/6796492/478656) and [Python - How to redirect stderr to a file?](http://stackoverflow.com/q/33500220/478656) and [Python: Redirect stdout and stderr to same file](http://stackoverflow.com/q/38776104/478656) and [Python. Redirect stderr to log file](http://stackoverflow.com/q/4673513/478656) – TessellatingHeckler Mar 28 '17 at 18:40
  • and [Redirecting stdout, stderror to file while stderr still prints to screen](http://stackoverflow.com/q/26347191/478656) and related [Redirect stdout to a file in Python?](http://stackoverflow.com/q/4675728/478656) and [Cleanly and optionally redirect stderr or stdout to file](http://stackoverflow.com/q/33291792/478656) – TessellatingHeckler Mar 28 '17 at 18:41

1 Answers1

2

In PowerShell, I would recommend using a transcript to log all output that would normally go to the host. (MSDN page)

You can use the -Append flag if you would like to add to whatever is already in the file instead of overwriting.

G42
  • 9,791
  • 2
  • 19
  • 34