1

I have some txt files in a directory and I need to get the last 15 lines from all of them. How could I do it using python?

I chose this code:

from os import listdir
from os.path import isfile, join

dir_path= './'
files = [ f for f in listdir(dir_path) if isfile(join(dir_path,f)) ]
out = []
for file in files:
    filedata = open(join(dir_path, file), "r").readlines()[-15:]
    out.append(filedata)
f = open(r'./fin.txt','w')
f.writelines(out)
f.close()

but I get the error "TypeError: writelines() argument must be a sequence of strings". I think it's because of Russian letters in the lines.

pnuts
  • 56,678
  • 9
  • 81
  • 133
Andrej
  • 77
  • 1
  • 6

4 Answers4

7
import os
from collections import deque

for filename in os.listdir('/some/path'):
    # might want to put a check it's actually a file here...
    # (join it to a root path, or anything else....)
    # and sanity check it's text of a usable kind
    with open(filename) as fin:
        last_15 = deque(fin, 15)

deque will automatically discard the oldest entry and peak the max size to be 15, so it's an efficient way of keeping just the "last" 'n' items.

mgilson
  • 283,004
  • 58
  • 591
  • 667
Jon Clements
  • 132,101
  • 31
  • 237
  • 267
1

Try this:

from os import listdir
from os.path import isfile

for filepath in listdir("/path/to/folder")
    if isfile(filepath): # if need
        last_five_lines = open(filepath).readlines()[-15:]

# or, one line:

x = [open(f).readlines()[-15:] for f in listdir("/path/to/folder") if isfile(f)]

Updated:

lastlines = []
for file in files:
    lastlines += open(join(dir_path, file), "r").readlines()[-15:]
with open('./fin.txt', 'w') as f:
    f.writelines(lastlines)
defuz
  • 25,583
  • 9
  • 37
  • 60
0
from os import listdir
from os.path import isfile, join

dir_path= '/usr/lib/something'
files = [ f for f in listdir(dir_path) if isfile(join(dir_path,f)) ]

for file in files:
    filedata = open(join(dir_path, file), "r").readlines()[-15:]
    #do something with the filedata
Thomas Schwärzl
  • 8,901
  • 6
  • 42
  • 67
0

Hope this helps:

import os

current_dir = os.getcwd()
dir_objects = os.listdir(current_dir)
dict_of_last_15 = {}
for file in dir_objects:
    file_obj = open(file, 'rb')
    content = file_obj.readlines()
    last_15_lines = content[-15:]
    dict_of_last_15[file] = last_15_lines
    print "#############: %s" % file
    print dict_of_last_15[file]
    file_to_check.close()
Kurt Campher
  • 685
  • 1
  • 5
  • 11