File Object behavior

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Apr 4 04:57:38 EDT 2007


Michael Castleton a écrit :
> 
> 
> Bruno Desthuilliers wrote:
>> Michael Castleton a écrit :
>>> When I open a csv or txt file with:
>>>
>>> infile = open(sys.argv[1],'rb').readlines()
>>> or 
>>> infile = open(sys.argv[1],'rb').read()
>>>
>>> and then look at the first few lines of the file there is a carriage
>>> return
>>> + 
>>> line feed at the end of each line - \r\n
>> Is there any reason you open your text files in binary mode ?
>>
(snip)


> Bruno,
> No particular reason in this case. It was probably as a holdover from using
> the csv module in the past.  I'm wondering though if using binary on very
> large
> files (>100Mb) would save any processing time - no conversion to system
> newline?
> What do you think?

I think that premature optimization is the root of all evil.

You'll have to do the processing by yourself then, and I doubt it'll be 
as fast as the C-coded builtin newline processing.

Anyway, you can easily check it out by yourself - Python has timeit (for 
micro-benchmarks) and a profiler.



More information about the Python-list mailing list