How to spawn a program and assign its stdout to a string variable?

Christopher R. Culver Kricxjo.neniuspamajxo at yahoo.com
Sat Nov 9 07:59:57 EST 2002


I'm working on a pdf2ps frontend script that will use the *nix utility
"file" to determine a file's type without blindly trying to work on it. I
assume the best way to do this is to spawn "file", capture its stdout to a
string variable, and then use regular expressions to search within the
captured stdout for the file type desired (in this case PDF). However,
I've tried these:

capturedstdout = os.system(["file"]+foo)

capturedstdout = swapvp('P_NOWAIT', 'file', ['file']+foo)

but these don't work because the return value of file is assigned to the
variable "capturedstdout", not the stdout. How do I spawn a program and
have its stdout assigned to a string variable?

Christopher Culver



More information about the Python-list mailing list