[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
Alan Gauld
alan.gauld at btinternet.com
Thu Sep 29 18:57:24 CEST 2011
On 29/09/11 15:22, lina wrote:
> I want to read a bunch of *.doc file in present working directory,
What format are the doc files?
If they are word processor files they may well be in binary format so
you will need to either decode them (using struct?) or find a module
that can read them, or a tool that can convert them to something you can
read.
Once you figure out how to read a single file reading multiple files can
be done in a number of ways including using os.walk() and a
for loop (or the fileinput module).
for root,dirs,files in os.walk(path):
docs = [f for f in files if f.endswith '.doc'] # or use glob
for line in fileinput.input(docs):
#process line
But the hardest bit is likely going to be the reading of the files if
they are not plain text.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list