a simple 'for' question

John McMonagle jmcmonagle at velseis.com.au
Tue Jul 8 21:02:08 EDT 2008


Ben Keshet wrote:
> Hi fans,
> 
> 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')
>    ...
> 

Use os.path.join


import os
folders = ['1A2B', '1A6W', '56Y7']
for x in folders:
    f = open(os.path.join('my/path/way', x, 'my_file.txt'), 'r')





More information about the Python-list mailing list