Created
March 17, 2018 10:22
-
-
Save okdolly-001/111c3ce740801bd8a082bab4321d288c to your computer and use it in GitHub Desktop.
Prepend line to beginning of a file #file #python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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