If I understand you correctly, your condition can be rewritten as: |distance_from_start_to_end - stated_length| < 10. Here is how to express this in Python:
with open('example.txt', 'r') as infile, \
open('output.txt', 'w') as outfile, \
open('errors.txt', 'w') as errfile:
for line in in file:
id_per, s_start, s_end, s_len = (line.split()[i] for i in [2, 3, 4, 5])
start_to_end = (int(s_end) - int(s_start)) + 1
if abs(int(s_len) - start_to_end) < 10:
outfile.write(line)
else:
errfile.write(line)
There are other improvements in this snippet with respect to your original code:
- use
with
, a context manager, to avoid having to close the file handles explicitly - you do not need the
re
module,split
can accept a tabulator as character to split by. - use
,
, the tuple operator, to deconstruct automatically the tokens splitted from the line - ignore fields by setting them to
_
- removed the
new_list
variable because it seems it is not used. Maybe I misunderstood your snippet?