Skip to content

Instantly share code, notes, and snippets.

@okdolly-001
Created March 17, 2018 10:22
Show Gist options
  • Save okdolly-001/111c3ce740801bd8a082bab4321d288c to your computer and use it in GitHub Desktop.
Save okdolly-001/111c3ce740801bd8a082bab4321d288c to your computer and use it in GitHub Desktop.
Prepend line to beginning of a file #file #python
#Prepend a line at the top of the file
def line_prepender(filename, line):
with open(filename, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write(line.rstrip('\r\n') + '\n' + content)
infile = "c_test.csv"
outfile = "complete_test.csv"
delete_list = ["b'", "'"]
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
line_prepender(outfile, li)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment