def prepend_line(filepath, line):
 with open(filepath) as f:
  lines = f.readlines()
 lines.insert(0, line)
 with open(filepath, 'w') as f:
  f.writelines(lines)

def prepend_line ( filepath , line ) : with open ( filepath ) as f : lines = f . readlines ( ) lines . insert ( 0 , line ) with open ( filepath , 'w' ) as f : f . writelines ( lines )
rewrite a file adding a line to its beginning .