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!

7
  • 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 ). For example this answer mentions that round(..) was fixed in 2.7 . Commented Dec 2, 2012 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... Commented Dec 2, 2012 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. Commented Dec 2, 2012 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. Commented Dec 2, 2012 at 21:45
  • @AndreHolzner a bit more code added Commented Dec 2, 2012 at 22:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ack! beat me to it by a minute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.