Gives me the following error message:
Traceback (most recent call last):
File “python”, line 1, in
IOError: [Errno 2] No such file or directory: ‘text.txt’
The error is in line 1 which is defined as per the instructions… I expect no error in the code… can someone please explain why the error pops up?
Just for future reference instead of looping with a traditional count controlled loop you can loop through the file like you would a string or an array like this:
my_file = open( "text.txt", "r" )
for line in my_file:
print( line )
my_file.close()
This way when you don’t know how many lines are in the file you can still iterate through them all.
Bandit, it says On three separate lines, print out the result of calling my_file.readline(). which is referring to the result that will be printed in the console, not your code. It’s more efficient to use a loop here.