Re: a simple 'for' question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Cook

    Re: a simple 'for' question


    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='Folde r/%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




    --
    Timothy Cook, MSc
    Health Informatics Research & Development Services
    LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook
    Skype ID == timothy.cook
    *************** *************** *************** *************** **
    *You may get my Public GPG key from popular keyservers or *
    *from this link http://timothywayne.cook.googlepages.com/home*
    *************** *************** *************** *************** **

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7 (GNU/Linux)

    iD8DBQBIdJQO2TF RV0OoZwMRAqntAK DMHzg+debOI82g0 +SRXClOLUGVewCf f29C
    C44vb9ffPU1WW7/5R8udU7U=
    =6FP3
    -----END PGP SIGNATURE-----

  • Bruno Desthuilliers

    #2
    Re: a simple 'for' question

    Tim Cook a écrit :
    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='Folde r/%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]
    Nope. Python's for loops iterate over elements, not over indices. Here,
    the following code:

    folders= ['1A28','1A6W',' 56Y7']
    for x in folders:
    print x
    filename='Folde r/%s/myfile.txt' %x
    print filename

    yields:

    1A28
    Folder/1A28/myfile.txt
    1A6W
    Folder/1A6W/myfile.txt
    56Y7
    Folder/56Y7/myfile.txt

    IOW: the problem is elsewhere.

    Comment

    Working...