Create Loop from Existing Code
Below is a code that will open a .csv file and insert a header at line '0'
What I want to do is create a loop so that I can point the code at a directory and loop through each file one by one. Each file in the directory have 23 million lines and I am running into memory errors when I try to write the loop. The code below works fine 开发者_Python百科for a single text file.
import os
L = list()
f = open(in.txt, 'rb')
for line in f.readlines():
L.append(line)
L.insert(0,"x,y\n")
f.close()
f1 = open(in.txt, 'wb')
for line in xrange(len(L)):
f1.write(L[line])
f1.close()
Yeah, don't do that. Open the new file, write out the header, use shutil.copyfileobj()
to copy the contents, then rename.
Readlines reads the entire file in to memory. Do this to read the file one line at atime.
for line in f:
L.append(line)
You don't need to use xrange in the second loop. You can loop directly over the array. You are incorrectly closing the file inside the loop. Try:
for line in L:
f1.write(line)
f1.close()
EDIT
Ignacio provided a much better solution to the problem, but in general, you should use my solution for iterating over lines in a file.
精彩评论