[Tutor] Making a python script to feed files into another pythonscript

Alan Gauld alan.gauld at btinternet.com
Tue Sep 11 09:47:41 CEST 2007


"Ashley Booth" <abooth at stanford.edu> wrote

> So far I can get it to read the directory fine but it ends up just
> opening the script I want the files to be fed into instead of giving
> it the input and output to run and running it. Any ideas?
>
> Here is what I have so far:
>
> import sys, os
>
> indir = raw_input('input directory path ') # input directory
> script1 = os.system(raw_input('python script path '))

os.system only returnms the error code of the operation
not the results, so script1 will usually contain 0 (for success).

You need to look at the subprocess module and the
examples of replacing popen. Specifically using the
communicate() method of Popen.

> for j in os.listdir(indir):
>       input = os.path.join(indir,j)
>       output = os.path.join(indir,j.split('rovctd')[0]+'.txt')

You might find os.path.basename more reliable here.

>       script1(input,output)

This won't work see above.

> If this is going to be too complicated,

No, this is exactly the kind of thing Python is good at.

> I would at least be happy with how to tack on the list
> directory stuff to then of the script1- it would not be a
> stand alone script but at least it would work.

Sorry you lost me there.
Which script? The one above or the ones you are
trying to execute?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list