file IO

Darren Dale dd55 at cornell.edu
Tue Aug 3 03:23:45 CEST 2004


Darren Dale wrote:

> Can anyone explain this?
> 
> I have a file called old.dat with two lines:
> 
> 1
> 2
> 
> So it's 3 bytes long. I run the following:
> 
> import os
> f = file('old.dat',mode='r')
> olddata = f.readlines()
> f.close()
> 
> f = file('new.dat',mode='w')
> f.writelines(olddata)
> f.close()
> 
> new.dat is now 4 bytes long. ???
> 
> I need to reformat and then save some data.   Then I need to be able to 
> export the reformatted data to a spreadsheet-friendly format. But once I 
> have simply copied (trying to isolate the problem) the file using the 
> script above, my export function takes 10x as long as it would have with 
> the original file. And worse, the output has an extra newline character 
> added at the end of each line. Any suggestions would really be 
> appreciated, I am going a bit crazy trying to understand this.
> 
> Darren

One more bit of info. The extra newline character is added to output 
when I open the rewritten file like this:

import os
from mmap import mmap, ACCESS_READ
f = file('foobar.dat',mode='rU')
fd = f.fileno()
m = mmap(fd, os.fstat(fd).st_size, None, ACCESS_READ)
olddata = []
line = m.readline()
while line:
     olddata.append(line)
     line = m.readline()

using mmap to read the original datafile works. Any thoughts? I would 
really like to stick with mmap, my datafiles are the right size to 
really benefit.

Darren



More information about the Python-list mailing list