Skip to main content

Posts

Showing posts from December, 2012

Python Pipe

Pipes can be used in python using the popen command. os.popen(command[, mode[, bufferSize]]). The default mode is read which can also be explicitly indicated by "r" as shown in the example below. The parameter bufferSize can be 0 (no buffering), 1 (line buffering), negative(system default buffering) and positive values greater than 1 (number defines the buffer size to be used). #Open a pipe to or from command import os # shell command to show all the files in current directory with ".py" in the file name shellCommand = ' ls | grep .py' pipe = os.popen(shellCommand,"r") while True: # read each output line line = pipe.readline() if not line: break print 'outputLine: ' print line