4 ms·
I like that you can slurp a file in two lines of Python. And for someone just learning Python, the author's solution is just unnecessarily complicated. How many
by j_baker 13y ago
I like that you can slurp a file in two lines of Python. And for someone just learning Python, the author's solution is just unnecessarily complicated. How many people learning Python are going to have a need to optimize file reading to this level?
And besides that, the author's solution is just good for one situation: when you want to read something line-by-line, which isn't always the case. For binary files, you may want to do something like this (untested code):
data = infile.read(256):
while data:
do_something(data)
data = infile.read(256)
Also, it seems like the author hasn't heard of enumerate: http://docs.python.org/2/library/functions.html#enumerate http://docs.python.org/2/library/functions.html#enumerate
- paulgb 13y agoI agree that it's good to have an easy way to read a whole file in, but when I think about it I can't think of any case where I've had to write code to read a file that I didn't want to process line-by-line, in which case the non-slurping method is actually less code.
- deleted 13y ago[deleted]
- mh- 13y agodepends on how much you care about performance/memory usage. for one, variance in the line lengths (by byte count) will probably force the usage of inefficient buffer sizes also, the parent comment gave one universal example: binary files
- repsilat 13y agoEven with binary files, the only reasonable ways I can think to process them are stream-wise and with mmap. (In fact, why would you ever slurp a file when you can mmap it instead?)
- j_baker 13y agoIt sounds like you've only read in line-by-line files then. I mean, it doesn't make sense to read in a JSON or XML document line-by-line. Nor does it make sense to read in most binary files line-by-line. Some formats, such as csv do make sense to read in line-by-line though.
- paulgb 13y agoI've read in lots of formats, including many XML and binary files. 99% of the time there is a library that already handles the low-level interaction with those files. The only times I've had to write original Python code that reads files directly have typically been things that could be processed line by line (eg various record-based data formats). I'm trying to think of a counter example but I'm coming up blank.
- vacri 13y agoAnd for someone just learning Python The context of the question and answers was 'a LinkedIn group for professional Python programmers'.
- rlwolfcastle 13y ago1. Get it to work 2. Test that it works 3. Optimize (if needed) If your file is small enough and is a trivial percentage of the overall run time, who cares how it is read?