a simple 'for' question
keshet1 at umbc.edu
Wed Jul 9 05:41:45 CEST 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.
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,
> 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 <mailto:bblais at bryant.edu>
> http://web.bryant.edu/~bblais <http://web.bryant.edu/%7Ebblais>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list