inplace text filter - without writing file

Sayth Renshaw flebber.crue at gmail.com
Sat Oct 1 20:21:24 EDT 2016


Hi

I have a fileobject which was fine however now I want to delete a line from the file object before yielding.

def return_files(file_list):
    for filename in sorted(file_list):
        with open(dir_path + filename) as fd:
             for fileItem in fd:
                 yield fileItem

Ned gave an answer over here http://stackoverflow.com/a/6985814/461887

for i, line in enumerate(input_file):
    if i == 0 or not line.startswith('#'):
        output.write(line)

which I would change because it is the first line and I want to rid <!--

for i, line in enumerate(input_file):
    if line.startswith(<!--'):
        output.write(line)

However I do not want to write the file I want to restore all the enumerated files back to the fileItem I need to yield.

How do I do this?

Cheers

Sayth



More information about the Python-list mailing list