Two problems with this code:
open("hw09-data-%d.csv", "r") % i
First of all, you're trying to apply the string formatting operator %
to the file object that open()
returns. You mean to be applying it to the filename string:
open("hw09-data-%d.csv" % i, "r")
Secondly, you mention filenames with two digit numbers, like hw-data-01
. "%d" % 1
is going to return "1"
though, not "01"
as you want. To fix this, you need to specify that you want a two-digit, zero-padded format, with "%02d"
open("hw09-data-%02d.csv" % i, "r")
Additional notes / critiques:
i = 00
You probably did this to try and fix your previous bug. But this accomplishes nothing in that way. In fact, this could be misleading if it weren't zero, because numbers that start with a leading zero are interpreted in octal. For example 0777
is actually the decimal number 511.
This is Python, not C! Iterate like a Python master:
for i in xrange(10):
with open("hw09-data-%02d.csv" % i, "r") as f:
for line in f :
print line