Perl-like backtick in Python
Tom Verbeure
tom_verbeure at hotmail.com
Thu May 2 17:57:43 EDT 2002
> Tom Verbeure wrote:
>
> > In Perl i can do something like this:
> >
> > $str = `cat myfile.in`;
> >
> > Basically the string that is surrounded by the backticks is executed
> > by a shell and the stdout is returned as a string.
> >
> > In the python library, there is spawn*, exec* and system, but the
> > return the exit status of the command, not the output. Any suggestions
> > about who to do this?
>
> You want os.popen, or one of the related beasts in the popen2 module.
That would be very ugly, wouldn't it?
But after a long hunt in the lib.pdf, I found the 'commands' package:
commands.getoutput("cat myfile.in")
does the trick!
Tom
More information about the Python-list
mailing list