[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
Marc Tompkins
marc.tompkins at gmail.com
Tue Oct 4 18:59:51 CEST 2011
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)
>
(in other words, split fileName, take just the first part, and add ".txt" to
the end of it.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111004/872ce6be/attachment-0001.html>
More information about the Tutor
mailing list