Discard content from a text file (with Python)

Recently I came across a scenario where I needed to process a text file so that a certain portion at the beginning was to be truncated up to a certain given line.

After doing a bit of digging, I came across a very elegant solution on StackOverflow which involved the use of itertools & lambda statements.

Say, for example, that we have the following text:

> Sed posuere urna vel risus scelerisque, sit amet sollicitudin arcu porta. Vestibulum nec posuere erat. Duis nec finibus magna. Phasellus efficitur tempus lacus suscipit tincidunt. Sed efficitur lacinia arcu, ac ullamcorper nisi consectetur eget. 
> Aliquam nisl eros, tincidunt non commodo ac, lobortis vel quam. Morbi ac magna in sapien imperdiet aliquet nec nec augue. Mauris ut sollicitudin dolor, ut venenatis sapien. Morbi nunc eros, sodales eget justo vel, suscipit porttitor sapien. 
> THIS IS A DELIMITER
> 
> Ut non eros purus. In fringilla ex arcu, eu vestibulum dui suscipit ac. Duis nec sagittis est. Praesent fringilla metus et congue placerat. Nulla viverra, purus quis mollis mollis, risus tortor interdum ante, id congue sem purus a orci. Vestibulum ante mi, pretium sollicitudin turpis vitae, malesuada finibus velit. Duis feugiat, urna a elementum ullamcorper, nulla felis pretium risus, sed porttitor metus dolor nec purus.

Now, we have the following code:

from itertools import dropwhile
import os
import sys

with open('path/to/your/text/file', 'r') as file:
    # Provide the text that marks the end of the block 
    #which will be truncated - in this example, it's 
    # "THIS IS A DELIMITER"
    dropped = dropwhile(lambda 
            _line: "THIS IS A DELIMITER" not in _line, file)
    next(dropped,"")

    for line in dropped:
    # You decide what to do with the remaining lines. 
    # In this example, we just print them out. 
    # Of course, these can be "forwarded" to another 
    # file handle
    print(line)

The outcome is a text stripped of all content up to (and including) the supplied „delimiter” (in this example, **THIS IS A DELIMITER**)

Finding files with Python

file_search

Just another entry in my Python-related scrapbook :). There are many occasions when having some code snippets related to file searches can prove useful.

Find for the first occurrence of a given filename in a given directory:

import os

def findFile(fileName, pathToSearch):
    for root, directories, files in os.walk(pathToSearch):
        if fileName in files:
            return os.path.join(root, fileName)

Find all files matching a given pattern and return a vector (list) with all entries (not that elegant yet, but it’ll do for the moment):

import os
import fnmatch

def findFileAllMatches(patternToFind, pathToSearch):
    results = []
        for root, dirs, files in os.walk(pathToSearch):
            for name in files:
                if fnmatch.fnmatch(name, patternToFind):
                    result.append(os.path.join(root, name))

    return results