a simple 'for' question
Tim Roberts
timr at probo.com
Thu Jul 10 03:34:06 EDT 2008
Ethan Furman <ethan at stoneleaf.us> wrote:
>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='Folder/%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 at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list