21__ 5/9 Urgent



I don't understand.
The error message is i need to refresh the page but nothing works.

mon_fichier = open("texte.txt","r+")
print mon_fichier.readline()
print mon_fichier.readline()
print mon_fichier.readline()

Oups, merci de réessayer. Désolé, mais vous allez devoir actualiser la page ! Ne vous inquiétez pas, votre code est sauvegardé. 

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


as you could have seen by the =error-message=

the file texte.txt which you are trying to read does not exist....

The solution would be to create a texte.txt file and retry your used read code


With following code
you will do a test of the existence of texte.txt file
and if the file texte.txt is not found
you will create the texte.txt file

import os
print "== if the file doesn't exist, create one =="
if os.path.exists('texte.txt'):
    print "File exists"
    print "Creating the texte.txt-file"
    my_file = open("texte.txt", 'w')
    my_file.write("I'm the first line of the file!" +"\n")
    my_file.write("I'm the second line."+"\n")
    my_file.write("Third line here, boss."+"\n")

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

== discussions / opinions ==


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