Is this a bug? (Reading Between the Lines)


#1

my_file = open("text.txt", "r")
print my_file.readline()
my_file.close()

Traceback (most recent call last):
File "python", line 1, in
IOError: [Errno 2] No such file or directory: 'text.txt'

Why this happens?


I m getting an error : Traceback (most recent call last): File "python", line 1, in <module> IOError: [Errno 2] No such file or directory: 'text.txt' . how should i solve it
#2

Hi @devmaster43954,

Yes, it is a bug.

There are several techniques that can compensate for the bug. One of them is to add this workaround to the beginning of the program in order to create the file ...

# Buggy exercise!
# This workaround will create the file.
f = open("text.txt", "w+")
f.write("I'm the first line of the file!\n")
f.write("I'm the second line.\n")
f.write("Third line here, boss.\n")
f.close()

Thereafter, you can open, read, and close the file.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.