a simple 'for' question

Brian Blais bblais at bryant.edu
Wed Jul 9 02:29:52 CEST 2008

On Jul 8, 2008, at Jul 8:8:08 PM, Ben Keshet wrote:

> I want to use a 'for' iteration to manipulate files in a set of  
> folders, something like:
> folders= ['1A28','1A6W','56Y7']
> for x in folders:
>    print x     # print the current folder
>    f = open('my/path/way/x/my_file.txt', 'r')
>    ...

I think:

f = open('my/path/way/%s/my_file.txt' % x, 'r')

should work, although I would probably be more clear about the names,  

for folder in folders:
     filename='my/path/way/%s/my_file.txt' % x

also, make sure that you do mean the relative path  my/path/way/...    
# in current folder
as opposed to the absolute path:  /my/path/way                         
# in root folder

Brian Blais
bblais at bryant.edu

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080708/33a49a6d/attachment.html>

More information about the Python-list mailing list