a simple 'for' question
Ethan Furman
ethan at stoneleaf.us
Thu Jul 10 11:34:35 EDT 2008
Tim Roberts wrote:
> 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'].
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.
More information about the Python-list
mailing list