views:

60

answers:

2

Hello all, I am using the following call for executing the 'aspell' command on some strings in Python:

r,w,e = popen2.popen3("echo " +str(m[i]) + " | aspell -l")

I want to test the success of the function looking at the stdout File Object r. If there is no output the command is successful.

What is the best way to test that in Python? Thanks in advance.

+2  A: 

Best is to use the subprocess module of the standard Python library, see here -- popen2 is old and not recommended.

Anyway, in your code, if r.read(1): is a fast way to test if there's any content in r (if you don't care about what that content might specifically be).

Alex Martelli
Thanks for the answer.
Spac
@Spac you're welcome, but I like Christopher's answer -- it's not a direct answer to your question but it may be a better architecture to solve your overall problem.
Alex Martelli
+2  A: 

Why don't you use aspell -a?

You could use subprocess as indicated by Alex, but keep the pipe open. Follow the directions for using the pipe API of aspell, and it should be pretty efficient.

The upside is that you won't have to check for an empty line. You can always read from stdout, knowing that you will get a response. This takes care of a lot of problematic race conditions.

Christopher
+1. This would also avoid potential shell injection attacks if the string being pasted to the command line is not sanitized. And avoid problems with running into maximum command line length limitations if the string is long.
Jukka Matilainen