Skip to content

Instantly share code, notes, and snippets.

@mtholder
Created December 31, 2019 17:41
Show Gist options
  • Select an option

  • Save mtholder/be325166940f374d2af65e6ccc09b71d to your computer and use it in GitHub Desktop.

Select an option

Save mtholder/be325166940f374d2af65e6ccc09b71d to your computer and use it in GitHub Desktop.
simple script to reverse the first n-1 lines of a file with n lines.
#!/usr/bin/env python3
'''Reverses the order of the first n-1 lines of a file
and writes it to stdout.
'''
import sys
fp = sys.argv[1]
with open(fp, 'r', encoding='utf-8') as inp:
lines = inp.readlines()
last = lines.pop(-1)
lines.reverse()
lines.append(last)
for line in lines:
sys.stdout.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment