5/9 error solved


#1

5/9 seems to throw an error for many. Seemingly the file has been deleted in a prev example or whatever, ie, there seems to be a bug for that section of the course than needs corrected. Anyway, here's how I got round it:

my_list = ["I'm the first line of the file!", "I'm the second line.", "Third line here, boss."]
forceFile = open("text.txt", "w")
for item in my_list:
forceFile.write(str(item) + "\n")
forceFile.close()

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


5. Reading Between the Lines - I think this might be a bug
#2

It looks like the text displays in text.txt, but for some reason the website doesn't read from it.
I highlighted the entire contents of text.txt, ctrl + x, ctrl + v, then save & submit code and it works fine


#3

@lennylux @chipace51599

Latter on you will have to handle issues like this with your programming in all languages. So you should get familiar with using try in python it is the perfect solution for this issue.

What the example does it to look for the file, if it does not find one it throws an error which we then catch and handle with some additional code. So if the file does not exist we create one.

Example

try:
    file = open("text.txt", "r+")
    print("File Opened")
except FileNotFoundError as error:
    print("You had an Error: %s" % error)
    print("Creating new text.txt file")
    file = open("text.txt", "w+")