Re: a simple 'for' question

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

    Re: a simple 'for' question

    Ethan Furman <ethan@stonelea f.uswrote:
    >Ben Keshet wrote:
    >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.
    >
    >The form of slash ('\' vs '/') is irrelevant to Python. At least on
    >Windows.
    >
    >folders= ['1','2','3']
    >for x in folders:
    > print x # print the current folder
    > filename='Folde r/%s/myfile.txt' %[x]
    ^- brackets not needed
    More than that, the brackets are CAUSING this problem. The "%" formatting
    operator expects to find either a single item, or a tuple containing
    multiple items. It does NOT look for a generic iterator. In this case,
    the %s will use the whole list as its parameter. Python converts the list
    to string, and the string representation of that one-item list is ['1'].
    > f=open(filename ,'r')
    >>
    >gives: IOError: [Errno 2] No such file or directory:
    >"Folder/['1']/myfile.txt"
    Just like that.
    >As far as the Python question of string substitution, "%s" % var is an
    >appropriate way.
    Right.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
  • Ethan Furman

    #2
    Re: a simple 'for' question

    Tim Roberts wrote:
    Ethan Furman <ethan@stonelea f.uswrote:
    >
    >
    >>Ben Keshet wrote:
    >>
    >>>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.
    >>
    >>The form of slash ('\' vs '/') is irrelevant to Python. At least on
    >>Windows.
    >>
    >>
    >>>folders= ['1','2','3']
    >>>for x in folders:
    >> print x # print the current folder
    >> filename='Folde r/%s/myfile.txt' %[x]
    >>
    > ^- brackets not needed
    >
    >
    More than that, the brackets are CAUSING this problem. The "%" formatting
    operator expects to find either a single item, or a tuple containing
    multiple items. It does NOT look for a generic iterator. In this case,
    the %s will use the whole list as its parameter. Python converts the list
    to string, and the string representation of that one-item list is ['1'].
    You are, of course, correct -- the brackets are causing the displayed
    problem below. But judging from the thread so far, there are actually
    multiple problems going on here, of which the brackets are only this one
    part. It looks like the OP also lacks understanding regarding absolute
    and relative path names, as well as (possibly) not knowing where his
    script/interpreter is running from.
    >
    >
    >> f=open(filename ,'r')
    >>>
    >>>gives: IOError: [Errno 2] No such file or directory:
    >>>"Folder/['1']/myfile.txt"
    >
    >
    Just like that.
    >
    >
    >>As far as the Python question of string substitution, "%s" % var is an
    >>appropriate way.
    >
    >
    Right.

    Comment

    Working...