0

I know it might sounds dumb to you but im learning Python right now by following course on the internet and they ask us to create a kind of labyrinth. They give a "base code", but when I try to run it, it says FileNotFoundError: [Errno 2] No such file or directory: 'cartes', the error is in os.listdir(cards). I would like the code to list the contents of a local directory named cartes but am unable to do so because I receive the FileNotFoundError.

This is the code that I'm struggling with:

# -*-coding:Utf-8 -*

"""This file contains the main code of the game.

Run him with Python to start the game.

"""

import os

from map import Map

# We load the existing maps
maps = []
for file_name in os.listdir("maps"):
    if file_name.endswith(".txt"):
        path = os.path.join("maps", nom_fichier)
        map_name = file_name[:-3].lower()
        with open(path, "r") as file:
            content = file.read()
            # Map creation, to complete

# We display the existing maps
print("Existing labyrinths :")
for i, map in enumerate(maps):
    print("  {} - {}".format(i + 1, map.nom))

# If there is a saved game, we display it, to complete

# ... Complete the program ...
Steven Black
  • 1,988
  • 1
  • 15
  • 25
Vincent Quirion
  • 389
  • 2
  • 4
  • 16

1 Answers1

2

Because the directory is local (you haven't specified the WHOLE file path, eg C:\Users....\roboc the script can only find this folder if it's in the same directory has been run.

Open terminal and navigate to the roboc folder. then run python roboc.py and that should provide a quick fix :)

Or try this

import os

from map import Map

# We load the existing maps
maps = []
script_path = os.path.dirname(os.path.realpath(__file__))
maps_path = os.path.join(script_path, "maps")
for file_name in os.listdir(maps_path):
    if file_name.endswith(".txt"):
        path = os.path.join(maps_path, nom_fichier)
        map_name = file_name[:-3].lower()
        with open(path, "r") as file:
            content = file.read()
            # Map creation, to complete

# We display the existing maps
print("Existing labyrinths :")
for i, map in enumerate(maps):
    print("  {} - {}".format(i + 1, map.nom))
Steven Black
  • 1,988
  • 1
  • 15
  • 25
  • thank you for your answer, but people are going to evaluate my work, so I would like it work by only running the file (without navigating in the terminal). – Vincent Quirion Jul 31 '18 at 23:54
  • check this answer out https://stackoverflow.com/questions/28799353/python-giving-filenotfounderror-though-the-file-exists they've explained it better than I can – Steven Black Jul 31 '18 at 23:54
  • If I understand, the error in this "answer" is when he tries to open the file (open (filename,'r')). I think my error is the path of "os.listdir()" and not of "with open()". What I wanna do is use a relative path (is this how you call it in english?) to get to the files. (Have you seen my folder image ?) – Vincent Quirion Aug 01 '18 at 00:43
  • yeah, that's fine you just need to run your script from the same directory as the folder your interested in. How are you running your script? – Steven Black Aug 01 '18 at 00:51
  • from the terminal (macOS), I write python3 ... (path) – Vincent Quirion Aug 01 '18 at 00:55
  • What I would like is to make it work only by writing python3 ... (path), if this is possible – Vincent Quirion Aug 01 '18 at 00:59
  • yeah, when you open the terminal try `cd ... (path to the python file)` then from there run `python3 roboc.py` – Steven Black Aug 01 '18 at 01:20
  • I know that but is there a way the person evaluating me won’t have to do that? Can I write something like os.chdir("current file") in the roboc.py? – Vincent Quirion Aug 01 '18 at 01:27
  • yeah - here's how to do that, let me update my answer https://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python – Steven Black Aug 01 '18 at 02:32