python subprocess store each line of output in a list
line-by-line, python, subprocess
Solution
\Is this what you want?
#!/usr/bin/env python
# src_dimensions.py
# requires mediainfo package
import subprocess, glob
globpattern = 'path/to/*.*'
cmd = ['mediainfo', '--Inform=Video;%Width%x%Height%\\n']
cmd.extend(glob.glob(globpattern))
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE)
outputlines = filter(lambda x:len(x)>0,(line.strip() for line in proc.stdout))
print outputlines
Problem
I'm trying to store the dimensions of video files in a folder into a list. ``` #!/usr/bin/env python # src_dimensions.py # requires mediainfo package import subprocess proc = subprocess.Popen('mediainfo --Inform="Video;%Width%x%Height%\n" /home/saad/Videos/*.*', shell=True, stdout=subprocess.PIPE) for line in iter(proc.stdout.readline,''): print line ``` The resulting output of this program is: ``` 384x288640x480480x350352x162480x360640x360480x360384x288240x180320x240480x360384x288346x26 ``` When I run the `mediainfo` command in the terminal, each video dimension is on a newline: ``` 384x288 640x480 480x350 352x162 480x360 ``` I want each dimension stored as a seperate item in a list. I'm trying to iterate over `stdout` but it doesn't work. UPDATE #1 ``` import subprocess dim = [] proc = subprocess.Popen('mediainfo --Inform="Video;%Width%x%Height%\\n" /home/saad/Videos/*.*', shell=True, stdout=subprocess.PIPE) for line in iter(proc.stdout.readline, ''): dim.append(line.rstrip('\n')) print dim ``` This seems to give me the list, thanks to @Chakib's suggestion. ``` ['440x360', '320x240', '480x360', '320x240', '480x360', '320x240', '320x240', '400x224', ''] ```