You can't just read/write into the same file simultaneously because you're inserting newlines every 6 characters. Those newlines will overwrite the next character in the file. Say the contents of the file are the following:
123456789
If you simply write a newline to the file after every 6th line, your file will be the following:
123456
89
Notice how the newline overwrote "7"
.
If your file is relatively small (a couple megabytes maybe), you can avoid creating a tempfile and just read the entire file into memory, set the buffer position back to 0, and overwrite it, like so:
with open(filename, 'r+') as f:
raw = f.read()
f.seek(0) #sets the buffer position back to the beginning of the file
for i in xrange(0, len(raw), limit):
line = raw[i:i+limit].rstrip('\n').replace('\n', ' ')
f.write(line + '\n')
If your file is very large, however, it makes sense not to load the entire data into memory, and instead write to a temp file, and copy afterwards:
with open(filename, 'r') as infile, open('tmp.txt', 'w') as outfile:
line =
while True:
line = infile.read(limit)
#if there is no more text to read from file, exit the loop
if not line:
break
outfile.write(line.rstrip('\n').replace('\n', ' ') + '\n')
import shutil
shutil.copyfile('tmp.txt', filename)