[Tutor] system()? popen2()? How to execute a command & save itsoutput?

Alan Gauld alan.gauld at btinternet.com
Thu Sep 30 11:04:26 CEST 2010

"James Hartley" <jjhartley at gmail.com> wrote

> I'm needing to transfer the following shell construct to Python, 
> plus save
> the output of execution:

Are you sure? It looks like you would be better writing a python 
using the ftp module. Shells are intended to execute external programs
but Python provides the tools to do the job directly from Python, with
no need to start external programs in most cases.

> FTP_SITE='ftp.somesite.com'
> ftp -a  $FTP_SITE <<EOF
> binary
> prompt off
> cd /some_dir
> dir
> bye

If you really really need to use an external process use the 
subprocess module.
But first check that you can't do it from within Python.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list