[Tutor] Reading text files
Remco Gerlich
scarblac@pino.selwerd.nl
Fri, 28 Apr 2000 02:19:08 +0200
On Thu, Apr 27, 2000 at 04:24:59PM -0700, John Choi wrote:
> I've got another text file question and was hoping I could glean more
> knowledge...
>
> Problem:
> Over a hundred text files by the following file name convention:
>
> quickYYYYMMDD.txt
>
> For each file, read the 1st and 7th line, and then write those lines to
> another file named aggy.txt in the following format:
>
> <1st line>, "~", <7th line>
>
> Any help or suggestions on how to go about doing this would be greatly
> appreciated. As it stands, I'm opening each file copy/pasting the lines.
I can't test here, but I think something like this works:
import glob
aggy = open('aggy.txt','w')
files = glob.glob('quick*.txt')
for file in files:
f = open(file,'r')
line1 = f.readline()
for i in range(6):
spam = f.readline() # Skip 5 lines
line2 = f.readline()
f.write('%s~%s' % (line1, line2))
f.close()
aggy.close()
Not sure if I got the format right though.
--
Remco Gerlich, scarblac@pino.selwerd.nl