Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 subprocess output

I want to run the Linux word count utility wc to determine the number of lines currently in the /var/log/syslog, so that I can detect that it's growing. I've tried various test, and while I get the results back from wc, it includes both the line count as well as the command (e.g., var/log/syslog).

So it's returning: 1338 /var/log/syslog But I only want the line count, so I want to strip off the /var/log/syslog portion, and just keep 1338.

I have tried converting it to string from bytestring, and then stripping the result, but no joy. Same story for converting to string and stripping, decoding, etc - all fail to produce the output I'm looking for.

These are some examples of what I get, with 1338 lines in syslog:

  • b'1338 /var/log/syslog\n'
  • 1338 /var/log/syslog

Here's some test code I've written to try and crack this nut, but no solution:

import subprocess  #check_output returns byte string stdoutdata = subprocess.check_output("wc --lines /var/log/syslog", shell=True) print("2A stdoutdata: " + str(stdoutdata)) stdoutdata = stdoutdata.decode("utf-8") print("2B stdoutdata: " + str(stdoutdata))     stdoutdata=stdoutdata.strip() print("2C stdoutdata: " + str(stdoutdata))     

The output from this is:

  • 2A stdoutdata: b'1338 /var/log/syslog\n'

  • 2B stdoutdata: 1338 /var/log/syslog

  • 2C stdoutdata: 1338 /var/log/syslog

  • 2D stdoutdata: 1338 /var/log/syslog

like image 614
user2565677 Avatar asked Aug 14 '13 23:08

user2565677


People also ask

How do I get output to run from subprocess?

To capture the output of the subprocess. run method, use an additional argument named “capture_output=True”. You can individually access stdout and stderr values by using “output. stdout” and “output.

What does Python Popen return?

Description. Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as in open() function.

What does subprocess Check_output return?

CalledProcessError Exception raised when a process run by check_call() or check_output() returns a non-zero exit status. returncode Exit status of the child process.


2 Answers

I suggest that you use subprocess.getoutput() as it does exactly what you want—run a command in a shell and get its string output (as opposed to byte string output). Then you can split on whitespace and grab the first element from the returned list of strings.

Try this:

import subprocess stdoutdata = subprocess.getoutput("wc --lines /var/log/syslog") print("stdoutdata: " + stdoutdata.split()[0]) 
like image 93
Joseph Dunn Avatar answered Sep 18 '22 08:09

Joseph Dunn


Since Python 3.6 you can make check_output() return a str instead of bytes by giving it an encoding parameter:

check_output('wc --lines /var/log/syslog', encoding='UTF-8') 

But since you just want the count, and both split() and int() are usable with bytes, you don't need to bother with the encoding:

linecount = int(check_output('wc -l /var/log/syslog').split()[0]) 

While some things might be easier with an external program (e.g., counting log line entries printed by journalctl), in this particular case you don't need to use an external program. The simplest Python-only solution is:

with open('/var/log/syslog', 'rt') as f:     linecount = len(f.readlines()) 

This does have the disadvantage that it reads the entire file into memory; if it's a huge file instead initialize linecount = 0 before you open the file and use a for line in f: linecount += 1 loop instead of readlines() to have only a small part of the file in memory as you count.

like image 23
cjs Avatar answered Sep 20 '22 08:09

cjs