Created
February 20, 2020 21:52
-
-
Save syedarehaq/4c145abecd7022793def5e4b2bc19132 to your computer and use it in GitHub Desktop.
Seeking a file at a specific byte position or location, usually useful to debug a BiguQuery export error
This file contains 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 argparse | |
parser = argparse.ArgumentParser() | |
parser = argparse.ArgumentParser(description="go to the line of a specific position") | |
parser.add_argument("input_fname", help = "The input csv file that we will be reading") | |
parser.add_argument("pos", help="position where we will be seeking the line", type=float) | |
args = parser.parse_args() | |
fname = args.input_fname | |
position = args.pos | |
try: | |
with open(fname) as file: # don't you go correcting me on naming it file. we don't call file directly anyway! | |
file.seek(position) # zero in base case | |
for line in file: | |
position += len(line) | |
print(line) | |
break | |
except: | |
# yes, a naked exception. TWO faux pas in one answer?!? | |
print('exception occurred at position {}'.format(position)) | |
raise # re-raise to see traceback or what have you |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment