Code example that I'm stuck on:
import sys
with open(sys.argv[1],'r') as infile:
num = 0
for line in infile:
num += 1
print num, line,
I've just started Python and gone through the most basic of basics, however reading some class notes I couldn't quite understand this part (pasted above). How does with
work and what is it doing here? as
seems to be linked with the with
in doing something but I can't quite make it out and what I've seen online doesn't make sense when compared to this. An explanation on how its used in this particular situation would really help.
Also this code is at the start of the input / output section of my notes, how is code using input and output?