2011年11月9日 星期三

subprocess module: Popen

# becomes


p1 = Popen(["cat","hello.txt"], stdout=PIPE)
p2 = Popen(["grep", "hello"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
outputList = p2.communicate()
#p2.communicate()[0] #stdout
#p2.communicate()[1] #stderr
#communicate()若呼叫第二次 會發生  I/O operation on closed file


p1.pid   #process id
p1.kill()#刪除process
retcode = call(["/bin/cat", "hello.txt"])
Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})


pipe = Popen("cat hello.txt", shell=True, stdout=PIPE).stdout
#得到的pipe為file,必須用file 操作方法
for line in pipe:
print (line)



>>
hello world 1 line


this is two line


and write to third line



沒有留言:

張貼留言