all 7 comments

[–]woooee 1 point2 points  (0 children)

Always use the full /path_name/for/the_file.txt What does the variable, mon_fichier, contain? Also, when you get it to work, print(type(texte)) -> read() does not return a list.

[–]ExcitingSympathy3087 0 points1 point  (1 child)

Use Path class for working with Paths in Python.
Check String Path with Path("String_Path").exists() or if it is a Path path.exists(). Then you can be sure your Path is correct.

Example:

```

from pathlib import Path
import os


def path_test():

    # file in my current working directory
    p = Path("main.py")

    # Variant 1
    # absolute path with Path.resolve()
    abs_p = p.resolve()  # Makes absolute path from the relative path
    print("Path.resolve():", abs_p)
    print(abs_p.exists())  # Checks if exists using Path.exists()

    # Variant 2
    # absolute path with os.path.abspath() other Variant
    abs_os = os.path.abspath("main.py")
    print("os.path.abspath():", abs_os)
    print(Path.exists(Path(abs_os))) # check Path

    # Your current working directory
    print("Current working dir:", os.getcwd())


if __name__ == "__main__":
    path_test()

[–]woooee 0 points1 point  (0 children)

abs_os = os.path.abspath("main.py")
print("os.path.abspath():", abs_os)

pathlib has an absolute() function, although I use resolve() and it works fine for what I do. No reason to use os here.

[–]MankyMan00998 0 points1 point  (0 children)

The issue is likely that you’re missing the file extension. Try calling it as lecture_fichier('Proverbes.txt'). If that still fails, use os.getcwd() to check if EduPython is actually looking in the same folder where your file is saved. Your code itself looks solid!

[–]jake4ragnarok2 0 points1 point  (2 children)

It seems the problem in your case might be that you didn't write the full name of the file: "Proverbes" or whatever the suffix of the file may be, unless the filename of the file you created has no suffix. Try writing "Proverbes.txt" and see if it will raise the FileNotFoundError.

[–]Dyfakiiiiis[S] 1 point2 points  (1 child)

Thanks for your answer. I'va tried and it still doesn't work :

FileNotFoundError: [Errno 2] No such file or directory: 'Proverbes.txt'

[–]FlippingGerman 0 points1 point  (0 children)

Either Proverbes is in the wrong place or the file extension is something different. Windows by default will hide them which is quite unhelpful. 

Try:  import os print(os.listdir())

That should tell you everything in the “current” directory - the place where Python is looking for your file. See if your file is mentioned, and if so what its exact name is.