开发者

Python error when using os.popen()

开发者 https://www.devze.com 2023-03-28 10:15 出处:网络
Well, I have a python script running on Mac OS X. Now I need to modify it to support updating my SVN working copy into a specified time. However, after learning I\'ve found that SVN commands only supp

Well, I have a python script running on Mac OS X. Now I need to modify it to support updating my SVN working copy into a specified time. However, after learning I've found that SVN commands only support updating the working copy into a specified version.

So I write a function to grub the information from the command: svn log XXX, to find the corresponding version to the specified time. Here is my solution:

process=os.popen('svn log XXX')
print process.readline()
print process.readline()
process.close()

To make the problem simple, I just print the first 2 lines in the output. However, when I was executing the script, I got the error message: svn: Write error: Broken pipe

I think that the reason why I got the message is that the svn command kept executing when I was closing the Popen. So the error message arise.

Is there any one who can开发者_如何转开发 help me slove the problem? Or give me a alternative solution to reach the goal. Thx!


I get that error whenever I use svn log | head, too, it's not Python specific. Try something like:

from subprocess import PIPE, Popen

process = Popen('svn log XXX', stdout=PIPE, stderr=PIPE)
print process.stdout.readline()
print process.stdout.readline()

to suppress the stderr. You could also just use

stdout, stderr = Popen('svn log XXX | head -n2', stdout=PIPE, stderr=PIPE, shell=True).communicate()
print stdout


Please use pysvn. It is quite easy to use. Or use subprocess.

Does you error still occur if you do finally:

print process.read()

And it is better to call wait() if you use os.popen or subprocess.

0

精彩评论

暂无评论...
验证码 换一张
取 消