Output 2/9


#1



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

Is there any moderator to tell me if there is a bug on this french version ?
Thank you.


mon_fichier = open("output.txt", "r+")

Oups, merci de réessayer. Avez-vous créé une variable nommée mon_fichier ? 

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


#2

@snakefinger33gmail.c

With
my_file = open("text.txt","r")

you are trying to open an existing file.....in read modus.....
If you use following code
you will see which files are available....

import os
print "==show us the existing files =="
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
    print str(f)
#my_file.close()

==================================================

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

import os
print "== if the file doesn't exist, create one =="
if os.path.exists('text.txt'):
    print "File exists"
else:
    print "Creating the text.txt-file"
    my_file = open("text.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("text.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

Hi Leonard.
I try to do it with your code but It doesn't still work.
Always the same message :

Oups, merci de réessayer. Il semblerait que mon_fichier ne soit pas un fichier.

and in the console :

== if the file doesn't exist, create one ==
File exists

Traceback (most recent call last):
** File "python", line 16, in **
NameError: name 'my_fichier' is not defined

I am disapointed because i need to pass this exercise to finish Python.

It seems to be a bug but thanks a lot.


#4

Hi again.
See your code with mon_fichier :

import os
print "== if the file doesn't exist, create one =="
if os.path.exists("text.txt"):
....print "File exists"
else:
....print "Creating the text.txt-file"

mon_fichier = open("text.txt", "w")
mon_fichier.write("I'm the first line of the file!" +"\n")
mon_fichier.write("I'm the second line."+"\n")
mon_fichier.write("Third line here, boss."+"\n")
mon_fichier.close()

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

i have done it with an indentation on line four and line six for the print.
In the console it works :

== if the file doesn't exist, create one ==
File exists
I'm the first line of the file!

I'm the second line.

Third line here, boss.

None

And see the error message :

Oups, merci de réessayer. Il semblerait que mon_fichier ne soit pas un fichier.

What do you think about that ?


#5

@snakefinger33gmail.c
I think you are right....there is a bug in the code-checker....
I have not found a solution.....

These are the checks done in the French version

test_functions":
try:
    mon_fichier
    except NameError:
        return "Avez-vous cr\u00e9\u00e9 une variable nomm\u00e9e "+
        "mon_fichier ?"
        
try:
    mon_fichier.search(1)
    except NameError:
        return "Il semblerait que mon_fichier n'ait "+
        "pas \u00e9t\u00e9 ouvert."
    except ValueError:
        return "Il semblerait que mon_fichier ne soit pas ouvert."
    except AttributeError:
        return "Il semblerait que mon_fichier ne soit pas un fichier."

if error:
    return "Votre syntaxe n'a pas l'air correcte. "+
    "Jetez un oeil aux exemples de gauche si vous "+
    "avez besoin d'aide !"
    
if not "r+" in code:    
    return "Assurez-vous d'ouvrir votre fichier pour "+
    "l'\u00e9criture et la lecture avec r+ !"
if not mon_fichier.close:    
    mon_fichier.close()
    return True"

===============================================

In the ENGLISH version the tests are

The open() Function
test_functions:
    try:    my_file
    except NameError:
        return "Did you create a variable called my_file?"
        
try:
    my_file.seek(1)
    except NameError:
        return "It looks like my_file hasn't been opened."
    except ValueError:
        return "It looks like my_file isn't open."
    except AttributeError:
        return "It looks like my_file isn't a file."

if error:
    return "Your syntax doesn't look quite right. "+
    "Check the example to the left if you need help!"
    
if not "r+" in code:
    return "Make sure you open your file for reading "+
    "and writing with r+!"
    
if not my_file.closed:    my_file.close()

return True

#6

Hi Leonard and thank you for your help.
I try to fix the bug with your code but it doesn't still work.
I am disapointed, i can't finish Python.

I show you the result with the french version :

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

File "python", line 3
except NameError:
^
SyntaxError: invalid syntax


#7

@snakefinger33gmail.c,
To code i presented is not a PYTHON code
but is the Javascript=script which is used by the code checker.....

But as you mentioned in your post
https://discuss.codecademy.com/t/2-9-resolved/48488/3?u=leonhard.wettengmx.n
you found a solution


#8

But i can't finish 5/9.


#9

@snakefinger33gmail.c
You can't finish because you hae no texte.txt file

As i pointed out
in previous Posts....
you could have created the 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()

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

#10

But i did it with it on 5/9 :
In the console :

==show us the existing files ==
text.txt
script.py
== if the file doesn't exist, create one ==
File exists
I'm the first line of the file!

I'm the second line.

Third line here, boss.

None

In the editor (with correct indentation)

import os
print "==show us the existing files =="
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
print str(f)

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

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

Error message :

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

**

Should i submit this code in the part 2/9 too ?

**


#11

@snakefinger33gmail.c
This exercise was looking for the texte.txt file as was asked in the Instructons...!!


#12

Two days ago i submit your code in 2/9 and it doesn't pass.
i found it
import re
mon_fichier = re.compile("fddf")
So i have removed your code and i passed this exercise only writing these 2 lines.
My problem now is the 5/9.
But perhaps my english is bad to explain you this problem.


#13

I don't understand anymore.


#14

Oups, merci de réessayer. Il semblerait que mon_fichier ne soit pas un fichier.

This is what happen it 2/9 when i use :

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

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


#15

@snakefinger33gmail.c
For the exercise 2/9 you will have to use the solution you found in
https://discuss.codecademy.com/t/output-2-9/48374/12?u=leonhard.wettengmx.n
In exercise 5/9 you can use my solution...but with filename texte.txt


#16

Oh yes !
Thanks a lot.
Now it works.
What an IDIOT i am !


#17

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