a simple 'for' question

Ben Keshet keshet1 at umbc.edu
Wed Jul 9 13:22:08 CEST 2008


it didn't help.  it reads the pathway "as is" (see errors for both 
tries).  It looks like it had the write pathway the first time, but 
could not find it because it searched in the path/way instead of in the 
path\way.  thanks for trying.

folders= ['1','2','3']
for x in folders:
    print x     # print the current folder
    filename='Folder/%s/myfile.txt' %[x]
    f=open(filename,'r')

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



Tim Cook wrote:
> On Wed, 2008-07-09 at 00:00 -0400, Ben Keshet wrote:
>   
>> 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 believe it's because x is the position marker what you want instead is
> the contents of folders at x; therefore folders[x] 
>
> HTH,
> Tim
>
>
>
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080709/54bee9a3/attachment.html>


More information about the Python-list mailing list