a simple 'for' question

Ben Keshet keshet1 at umbc.edu
Wed Jul 9 00:00:38 EDT 2008


oops, my mistake, actually it didn't work...
when I tried:
for x in folders:
    print x     # print the current folder
    filename='Folder/%s/myfile.txt' %x
    f=open(filename,'r')

it says: IOError: [Errno 2] No such file or directory: 'Folder/1/myfile.txt'

I should mention that I am working in windows, could it be the / vs. \? 

I tried:
for x in folders:
    print x     # print the current folder
    filename='Folder\%s\myfile.txt' %x
    f=open(filename,'r')

filename is now 'Folder\\1\\myfile.txt', and the pathway doesn't exist.  
Any suggestions?

Thanks again 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/20080709/0f931cbd/attachment.html>


More information about the Python-list mailing list