[Tutor] Making a python script to feed files into anotherpythonscript

Alan Gauld alan.gauld at btinternet.com
Wed Sep 12 19:51:03 CEST 2007


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

> John- I was not sure what you meant by "command line". I took what 
> you
> said quite literally and created this:
>
> indir = raw_input('input directory path ') # asks for input 
> directory
> script1 = raw_input('python script path ') # asks for path to 
> desired script
>
> for j in os.listdir(indir):
>       input = os.path.join(indir,j)
>       output = os.path.join(indir,j.split('rovctd')[0]+'.txt')
>       os.system('python %s %s %s' % (script1, input, output)

Count the parens, you are one short...

> which does not run and I was confused by all the % symbols. I think 
> I
> completely misinterpreted your comment.

This uises a "format string2 which is a Python feature enabling you to
insert values into a string. In this case the string takes three 
values,
all themselves strings - thats what %s means. The bit after the % on
its own is the tuple of values to be inserted, in this case the values
of script1, input and output.

See the simple sequences topic in my tutorial for more info.

> Alan-I am confused about how to used the communicate() module. Could
> you please give me an example?
>
> the format should be(?):
>
> communicate(input=None)

Look at the subprocess module for lots of examples of how to use
the Popen class. It has a method called communicate which allows
you to communicate with the running process and access its output.

This is explained fuerther in the Using the OS topic in my tutorial...
about three quarters down under the heading Running an External 
Program

> What I was trying to say before at the end of my post:
>
>> I would at least be happy with how to tack on the list
>> directory stuff to THE END of the script1- it would not be a
>> stand alone script but at least it would work.
>
> In other words, instead of having a stand alone script that feeds
> files into script1 (the program I want the files to be fed into-it
> reformats files so it requires a file path input and output),

This is the bit that confuses me. You want the initial program
to reformat the files (which files?) so that it requires a path 
instead
of a filename?

> have script1 ask me for a directory instead of a specific input and
> output file path as it does now.

Sure, but it doesn't win you much if any advantage.

> Then it would theoretically go
> through and reformat each file in that directory, rename them and 
> put
> the formatted files back in that directory.

Why not just do that in the tiop level script?
Or do you want a top level script that will do something like run
any given script/command on any given file pattern?
In other words a Python version of the unix find -exec command?

Can you give us a sample user session to sjhow what this
would look like if you got it working?

Regards,

-- 
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