[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
lina
lina.lastname at gmail.com
Tue Oct 4 19:07:01 CEST 2011
On Wed, Oct 5, 2011 at 12:59 AM, Marc Tompkins <marc.tompkins at gmail.com>wrote:
> On Tue, Oct 4, 2011 at 8:48 AM, lina <lina.lastname at gmail.com> wrote:
>
>> if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
>> filedata = open(fileName)
>> text=filedata.readlines()
>> for line in text[0:]:
>> result.append({t:line.strip().count(t) for t in tokens})
>> for index,r in enumerate(result):
>> outfiledata=open("fileName.txt","w").write(index,"-----",r)
>> I still have problem using the value of the fileName,
>>
>> here the output is fileName.txt, not $fileName.txt which is supposed to be
>> 1.txt following input 1.xpm
>>
>
> That's because in this line:
>
>> outfiledata=open("fileName.txt","w").write(index,"-----",r)
>>
> you put "fileName.txt" in quotes - which makes it a literal, not a
> variable.
>
> Try this:
>
>> outfiledata=open(os.path.splitext(fileName)[0] +
>> ".txt","w").write(index,"-----",r)
>>
> Thanks, it works. I understand it now.
> (in other words, split fileName, take just the first part, and add ".txt"
> to the end of it.)
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Best Regards,
lina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111005/025c5d31/attachment.html>
More information about the Tutor
mailing list