0

Are there any potential differences between Python versions using open. My script accepts a file, performs some calculations and outputs these calculations.

On my machine (running 2.7.1) the output is correct. On the other machine (running 2.4.6) my output is all 0.00. Nearest I can tell, my input file isn't being opened. Is there a difference between open() between these versions?

Here's what my current open scheme looks like:

infile = open(filename, 'r')

An additional question: the next(infile) method does not skip the first line for me anymore in 2.6.4. Does replacing next(infile) with lines = infile.readlines()[1:0] affect performance?

EDIT: CRAP THE VERSION OF THE OTHER MACHINE IS 2.4.6. Not sure how I messed that up. The original post reflects my screwup, sorry guys.

Edit2: Below is my code for reading from the file:

     for lines in infile:
          # do stuff with lines
     infile.close()

I'm guessing there's something here that may be different between 2.4.6 and 2.7.1

FINAL EDIT:

Solved my own problem guys. next(infile) is not compatible with 2.4.6 for some strange reason. I replaced it and ran my script correctly.

Thanks for your help!

Incara
  • 1
  • 4
  • it's unlikely to be related to the `open(..)` function, you should post the details of your calculation code (the full list of release notes is [here](http://svn.python.org/projects/python/tags/r27/Misc/NEWS) ). For example [this answer](http://stackoverflow.com/a/8582986/288875) mentions that `round(..)` was fixed in 2.7 . – Andre Holzner Dec 02 '12 at 21:29
  • If you have two separate questions you should ask two separate questions :). An easy way to check performance is to use `%timeit thing_to_test` in ipython... – Andy Hayden Dec 02 '12 at 21:29
  • I'm guessing it's an issue with the data file. Python didn't change that significantly between 2.6 and 2.7, and if you were using a 2.7 only feature that's not in 2.6 it would probably complain, not silently give you the wrong answer. – chmullig Dec 02 '12 at 21:30
  • The version of the machine I need it to work is running 2.4.6. My apologies. An edit reflects my mistake. – Incara Dec 02 '12 at 21:45
  • @AndreHolzner a bit more code added – Incara Dec 02 '12 at 22:10
  • My guess is that you put `infile.readlines()` before the loop which exhausted the file (instead of progressing it one line like `next()`) and that's why you get `0.00` as there no lines to process (`readlines` ate them all). – lqc Dec 02 '12 at 22:38
  • I see, I see. So how can I skip the first line of a file in 2.4 and still read the file? – Incara Dec 02 '12 at 22:46

1 Answers1

1

I don't believe such changes have been made to open.

Regarding the second half of your question,

lines = infile.readlines()[1:0] is always [], what you probably need is lines = infile.readlines()[1:] instead.

And yes, using readlines loads the entire file (all its lines, to be precise) in memory when using next only reads the first line (and discards it if you don't do some_thing = next(f)).

If you're reading a large file, this can have an impact on performance. With a small file, it won't really make much difference.

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116