views:

51

answers:

4

I have a directory with 260+ text files containing scoring information. I want to create a summary text file of all of these files containing filename and the first two lines of each file. My idea was to create two lists separately and 'zip' them. However, I can get the list of the filenames but I can't get the first two lines of the file into an a appended list. Here is my code so far:

# creating a list of filename
for f in os.listdir("../scores"):
    (pdb, extension) = os.path.splitext(f)
    name.append(pdb[1:5])

# creating a list of the first two lines of each file
for f in os.listdir("../scores"):
    for line in open(f):
        score.append(line)
        b = f.nextline()
        score.append(b)

I get an error the str had no attribute nextline. Please help, thanks in advance.

+4  A: 

The problem you're getting is a result of trying to take more than one line at a time from the scores file using a file iterator (for line in f). Here's a quick fix (one of several ways to do it, I'm sure):

# creating a list of the first two lines of each file
for f in os.listdir("../scores"):
    with open(f) as fh:
        score.append(fh.readline())
        score.append(fh.readline())

The with statement takes care of closing the file for you after you're done, and it gives you a filehandle object (fh), which you can grab lines from manually.

perimosocordiae
Thanks! Worked a treat, now its working I put both parts under the same loop. Cheers!
+1  A: 

File objects have a next() method not nextline().

msw
A: 

Merging comment from David and answer from perimosocordiae:

from __future__ import with_statement
from itertools import islice
import os
NUM_LINES = 2
with open('../dir_summary.txt','w') as dir_summary:
  for f in os.listdir('.'):
    with open(f) as tf:
      dir_summary.write('%s: %s\n' % (f, repr(', '.join(islice(tf,NUM_LINES)))))
Adam Bernier
A: 

Here is my maybe more old fashioned version with redirected printing for easier newlines.

## written for Python 2.7, summarize filename and two first lines of files of given filetype
import os
extension = '.txt' ## replace with extension of desired files
os.chdir('.') ## '../scores') ## location of files

summary = open('summary.txt','w')
# creating a list of filenames with right extension
for fn in [fn for fn in os.listdir(os.curdir) if os.path.isfile(fn) and fn.endswith(extension)]:
    with open(fn) as the_file:
        print >>summary, '**'+fn+'**'
        print >>summary, the_file.readline(), the_file.readline(),
        print >>summary, '-'*60
summary.close()
## show resulta
print(open('summary.txt').read())
Tony Veijalainen