Created
July 19, 2018 17:16
-
-
Save Kasahs/9e4069000c2ee7a2721760c76a89cc2d to your computer and use it in GitHub Desktop.
read lines from file in reverse order and write to stdout till match is found
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
def readlines_reverse(filename): | |
with open(filename) as qfile: | |
qfile.seek(0, os.SEEK_END) | |
position = qfile.tell() | |
line = '' | |
while position >= 0: | |
qfile.seek(position) | |
next_char = qfile.read(1) | |
if next_char == "\n": | |
yield line[::-1] | |
line = '' | |
else: | |
line += next_char | |
position -= 1 | |
yield line[::-1] | |
def main(): | |
infile_path = sys.argv[1] | |
queries = str(sys.argv[2]).split(',') | |
for line in readlines_reverse(infile_path): | |
found = True | |
for query in queries: | |
if line.find(query) <= -1: | |
found = False | |
break | |
if found: | |
break | |
print(line) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment