I want to grep the error's out of a log file and save the value as an error. When I use:
errors = os.system("cat log.txt | grep 'ERROR' | wc -l")
I get the return code that the command worked or not. When I use:
errors = os.popen("cat log.txt | grep 'ERROR' | wc -l")
I get what the command is trying to do.
When I run this in the command line I get 3 as thats how many errors there are.
Can anyone suggest another way in Python that will allow me to save the value of this bash command?
Thanks
popen
is deprecated. Use subprocess instead. For example, in your case:
p1 = Popen(["cat", "log.txt"], stdout=PIPE)
p2 = Popen(["grep", "ERROR"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With