a simple 'for' question

Ben Keshet keshet1 at umbc.edu
Tue Jul 8 23:41:45 EDT 2008


thanks for the reply - it worked.  I just want to make sure that I 
understand the general syntax - when can I use the iteration variable as 
x (e..g. print x), and when do I need to use the ...%s... %x syntax? is 
it only when I need to call x as a "string"?

I am not sure I am asking the question right or using the right terms as 
I am fairly new to programing and python.

Thanks for your time.
Ben

Brian Blais wrote:
> 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, 
> like:
>
> folders=['1A28','1A6W','56Y7']
> for folder in folders:
>     filename='my/path/way/%s/my_file.txt' % x
>     fid=open(filename,'r')
>     ....
>
> 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
>
>
> bb
> -- 
> Brian Blais
> bblais at bryant.edu <mailto:bblais at bryant.edu>
> http://web.bryant.edu/~bblais <http://web.bryant.edu/%7Ebblais>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080708/f6fdf1ee/attachment-0001.html>


More information about the Python-list mailing list