[Tutor] Parsing data from a set of files iteratively

Spyros Charonis s.charonis at gmail.com
Sat May 19 13:42:36 CEST 2012

I have tried the following two snippets which both results in the same

import os, glob
homology_models = glob.glob('*.pdb')
for i in range(len(homology_models)):
python serialize_PIPELINE_models.py homology_models[i]

import os, sys
path = "/users/spyros/desktop/3NY8MODELSHUMAN/
dirs = os.listdir(path)
for file in dirs:
python serialize_PIPELINE_models.py

The error, respectively for each snipped, read:

File "<stdin>", line 2
    python serialize_PIPELINE_models.py homology_models[i]
SyntaxError: invalid syntax

 File "<stdin>", line 2
    python serialize_PIPELINE_models.py
SyntaxError: invalid syntax

In the first snippet, the final line reads:
'python' (calling the interpreter) 'serialize_PIPELINE_models.py' (calling
my python program) 'homology_models[i]' (the file to run it on)

the glob.glob routine returns a list of files, so maybe python does not
allow the syntax "python (call interpreter)" "list entry" ?

Many thanks.

On Fri, May 18, 2012 at 7:57 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

> On 18/05/12 19:23, Spyros Charonis wrote:
>> Dear Python community,
>> I have a set of ~500 files which I would like to run a script on.
> > ...Is there a way in which I can iteratively execute my script
> > over all 500 files
> Yes.
> You could use os.walk() or the glob module depending on whether
> the files are in a folder heirarchy or a single folder.
> That will give you access to each file.
> Put your functionality into a function taking a single file
> as input and a list to which you append the new data.
> Call that function for each file in turn.
> Try that and if you get stuck come back with a more specific question, the
> code you used and the full error text.
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120519/07fd23f6/attachment.html>

More information about the Tutor mailing list