9/9 weird thing


#1

The code says that we open the file as "textfile". But as I move through the instructions they say that we should refer to the file as my_file. Moreover if I try to refer to the file as "textfile" i get an error.


#2

I haven't got there yet, but it would seem that "textfile" tells python what kind of file it is that you wish to open, so it can use the correct ?codec? (not sure of the correct term here)
the file itself is labeled my_file.
its like the way ms-dos recognises filename extensions.
you could also write a code stating "if my_file ends in *.txt, then file type is textfile"
BTW, that is not real coding, just a way of expressing what python is doing.


#3

I didnt get any errors with "textfile", so i think its problem in your code.

with open("text.txt", 'w') as textfile:
    textfile.write("Donuts")
if not textfile.closed:
    textfile.close()
print textfile.closed

#4

It's about being consistent I think - although in my case it wouldn't accept my code unless I changed textfile to my_file.

with open("text.txt", "r+") as my_file:
    my_file.write("Lads")
    
if my_file.closed != True:
    my_file.close()

print my_file.closed

#5

Nope sorry, that's not it. They were just making things a little confusing :wink:


#6

Ran into the same problem, messed me up for a good 20 mins haha. This was helpful thanks, finally got it to run.


#8

esto funciona:

with open("texto.txt","w") as mi_archivo:
mi_archivo.write("hola")

if not(mi_archivo.closed):
mi_archivo.close()
print mi_archivo.closed