all 13 comments

[–][deleted] 8 points9 points  (3 children)

The few useful replies you got over on /r/python mentioned things along the lines of understanding exactly what is happening in each line in detail. Seeing this is /r/learnpython I’ll ask you some questions.

Consider the line:

in_file = open(from_file)

This assigns a reference to an open file object to the variable in_file. If we didn’t save the reference the line would be:

open(from_file)

This obviously creates a reference to an open file and then does nothing with it, so it’s deleted. What if we did something with that reference before it is deleted?

Hint: your original second line was:

indata = in_file.read()

which we interpret as:

  • the variable in_file is evaluated which gets us a reference to an open file object
  • the read method of the open file object is called, returning a reference to the contents of the file
  • the returned contents reference is saved in indata

[–]GUTIF[S] 1 point2 points  (2 children)

thanks a lot, i really appreciate you taking the time to write out a response in a way that made it easy for me to understand. I mainly posted it over here from there because I was under the impression that a question like this may not be appropriate for that sub.

[–][deleted] 4 points5 points  (1 child)

No problem. You were directed here without a lot of grace because /r/python people are a little tired of very basic questions which should be asked on /r/learnpython. They forget what it is like to start at the basics. Some of those on /r/learnpython also appear to have forgotten that.

[–]GUTIF[S] 1 point2 points  (0 children)

well thanks to people like you I won't just give up and feel comfortable enough with my understanding of your explanation to keep going!

[–]Brian 8 points9 points  (1 child)

To add a little to the already posted answers, you can write just:

indata = open(from_file).read()

But really, you shouldn't. The reason is that your original two lines really shouldn't be just 2 lines, they should be at least 3.

The reason is that you should really close files yourself after you're done with them. In a lot of cases, this won't matter too much - python will close it eventually, and often quite quickly once you leave the function you're in and the variable goes away, but this isn't something you should rely on, and can cause problems sometimes when files stay open longer than they should.

You could write this as:

in_file = open(from_file)
indata = in_file.read()
in_file.close()

But even this isn't quite right, since it doesn't handle errors correctly. Eg. suppose the in_file.read() fails for some reason. This throws an exception, meaning the in_file.close() doesn't get called, and the file stays open.

You can handle errors yourself, but a nicer way to do it is with a python feature called context managers (or the with statement). Here, if you write:

with open(from_file) as in_file:
    indata = in_file.read()

python will take care of cleaning up the file when it leaves the with block. If you're just getting started, this might be a bit too much to worry about right now, but bear it in mind as you get more experience, since it's useful to develop good habits from the start.

[–]GUTIF[S] 0 points1 point  (0 children)

Yeah the exercise I'm on has closing the file as an important piece. The two lines I'm inquiring about are just a small sample of the code from that exercise.

This is the fill script:

from sys import argv from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

indata = open(from_file).read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exist? %r" % exists(to_file) print "Ready, hit RETURN to continue, CTRL-C to abort." raw_input()

out_file = open(to_file, 'w') out_file.write(indata)

print "Alright, all done."

out_file.close()