[Tutor] Parsing data from a set of files iteratively

Alan Gauld alan.gauld at btinternet.com
Sat May 19 14:36:58 CEST 2012


On 19/05/12 12:42, Spyros Charonis wrote:
> I have tried the following two snippets which both results in the same
> error
>
> import os, glob
> os.chdir('users/spyros/desktop/3NY8MODELSHUMAN/')
> homology_models = glob.glob('*.pdb')
> for i in range(len(homology_models)):
> 	python serialize_PIPELINE_models.py homology_models[i]

OK, now we see the problem.

You are trying to invoke the interpreter from within
the interpreter. You don't do that.

Instead you need to call the functions defined inside 
serialize_PIPELINE_models.py from your script. To do that
you will need to import serialize_PIPELINE_models.py using

import serialize_PIPELINE_models

or, since its a long name:

import serialize_PIPELINE_models as spm

Than you can access the code functions inside using

spm.serialise(filename)

Or whatever the functions inside serialize_PIPELINE_models.py are.

Another thing which makes life easier is to use the for loop differently.
Instead of:
 > homology_models = glob.glob('*.pdb')
 > for i in range(len(homology_models)):

Just use:

  for fn in glob.glob('*.pdb'):
         spm.serialise(fn)


But you will first need to ensure that the code inside 
serialize_PIPELINE_models.py is available to use as functions.

The other option of course is to miss out Python at this level and write 
a bash script to call Python. It would look something like:

#!/bin/bash
###########################
cd users/spyros/desktop/3NY8MODELSHUMAN/
for file in *.pdb
do
  	python serialize_PIPELINE_models.py $file
done
############################

Which might be the simplest thing if the serialize_PIPELINE_models.py
already works as you require.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list