21__ 5/9 Urgent


#1



https://www.codecademy.com/fr/courses/python-intermediate-fr-FR-i3pwf/1/1?curriculum_id=5370f030fed2a866a2000001


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()
mon_fichier.close()

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'


#2

@snakefinger33gmail.c
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"
else:
    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.close()

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

Reference:
== discussions / opinions ==
http://stackoverflow.com/questions/11968976/list-files-in-only-the-current-directory
http://stackoverflow.com/questions/18533621/creating-a-new-text-file-with-python
http://stackoverflow.com/questions/5214578/python-print-string-to-text-file


#3

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