Skip to content

Instantly share code, notes, and snippets.

@Visgean
Created October 5, 2015 22:31
Show Gist options
  • Save Visgean/49278f2b87839af9a38c to your computer and use it in GitHub Desktop.
Save Visgean/49278f2b87839af9a38c to your computer and use it in GitHub Desktop.
import os
def get_block_range (filename, lookupValue):
"""
Returns the index of the first line that contain ``lookupValue``
and end of the file
# here the value was at line 941 and the file had 1000 lines.
>>> get_block_range('example.txt', 'example')
(941, 1000)
:rtype: tuple
"""
if not os.path.getsize(filename):
return None, 0
with open(filename, 'r') as file:
lookup_position = None
for num, line in enumerate(file, 1):
if lookup_position is None and lookupValue in line:
lookup_position = num
return lookup_position, num
asx
asx
as
x
asxfsdvsf
gbs
bs
dfv
sdfvsdf
bsb
dfv
example
gsf
b
fg
bsf
gb
sfgb
sfg
b
sfgb
sf
bg
sfg
b
In [10]: asx.get_block_range('example.txt', 'example')
Out[10]: (12, 27)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment