May-30-2017, 11:39 AM
Hello all. I am using subprocess module to execute a process and read stdout returned by it. Currently my code saves all the lines of stdout to a list ("lines" in the code below). However, I am interested only in first 3 lines of the stdout. The only way I managed to get first 3 lines was by simply taking first 3 items from the list. But I am wondering whether there is a more elegant and efficient way. One which would only have subprocess "extract" a specific number of stdout lines already to start with? Thanks. JC
process = subprocess.Popen("top -n 1", stdout=subprocess.PIPE, shell=True) output = process.stdout.read() output_lines = output.split(b'\n') lines = []