Question : How to append a header line to a file?

How to append a single line at tail position to an existing file?

Answer : How to append a header line to a file?

Huh? "tail position" normally means at the end... ;)

You can't really append something at the top of the file. You would have to rewrite the entire file, which means it will be slow when the file is huge. But except from that, it is not  problem:
1:
2:
3:
4:
5:
6:
filename = 'thefile.txt'
lines = file(filename).readlines()
new_lines = ['one more line at the top\n'] + lines
f = open(filename,'w')
f.writelines(new_lines)
f.close()
Open in New Window Select All
Random Solutions  
 
programming4us programming4us