1

I am facing a problem of file not found. os.listdir() method should be able to load folder. Why it cannot work correctly? Make me any advice and suggestions. Thank you.

scene = 'scene1'
folders = os.listdir("graph_state_list/" + scene + "/")
for folder in folders:
    try:
        activity_directory = "graph_state_list/" + scene + "/" + folder
        directories = os.listdir(activity_directory)
        program_discription_list = []
        for directory in directories:    
            program_description_path = "graph_state_list/" + scene + "/" + folder + "/" + directory + "/program-description.txt"
            program_description = {}
            input_file = open(program_description_path, "r")
            name_desc = []
            for line in input_file:
                name_desc.append(line.strip())
            input_file.close()
            program_description = {
                "name": name_desc[0],
                "description": name_desc[1]
            }
            program_discription_list.append(program_description)
            activity_program = get_activity_program("graph_state_list/" + scene + "/" + folder + "/" + directory + "/activityList-program.txt")
            graph_state_list = get_graph_state_list("graph_state_list/" + scene + "/" + folder + "/" + directory + "/activityList-graph-state-*.json")
            create_rdf(graph_state_list, program_description, activity_program, scene, directory)
    except Exception as e:
        print(e.args)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Input In [66], in <cell line: 2>()
      1 scene = 'scene1'
----> 2 folders = os.listdir("graph_state_list/" + scene + "/")
      3 for folder in folders:
      4     try:

FileNotFoundError: [Errno 2] No such file or directory: 'graph_state_list/scene1/'
Easter012
  • 29
  • 4
  • 1
    Is `graph_state_list` a sub directory of where your script is executed? The error typically comes whenever there's something wrong with the directory structure. You could try to `print(os.getcwd())` – Cow Jul 07 '22 at 11:18
  • @user56700, Thank you for you advice. The results of print(os.getcwd()) is /home/username/virtualhome2kg/demo. graph_state_list is located under the folder of demo. – Easter012 Jul 07 '22 at 11:44
  • 2
    Please first extract a [mcve]. In general though, if it claims the file is not there but you see the file, you got the filename/path wrong. For relative paths, it's getting additional complications (vote for https://github.com/python/cpython/issues/93431 if you like). Oh, BTW, take a look at the `pathlib` module, which is better than treating paths as simple strings. – Ulrich Eckhardt Jul 07 '22 at 11:50

3 Answers3

1

The problem is with the slash character you have in your path.

Try to use this instead:

activity_directory = "graph_state_list\\" + scene + "\\" + folder

EDIT: the error is raised in line 2, so please change that one as well to:

folders = os.listdir("graph_state_list\\" + scene + "\\")
1

May I suggest using os.path.join() in order to use the correct folder structure:

import os

current_dir = os.getcwd()
scene = 'scene1'
folders = os.path.join(current_dir, "graph_state_list", scene)
print(folders)
for folder in os.listdir(folders):
    ...

Result (folders):

C:\Users\xxx\OneDrive\BACKUP\Python\graph_state_list\scene1
Cow
  • 2,543
  • 4
  • 13
  • 25
  • 1
    You are already in `os.getcwd`; this is a common beginner antipattern. See also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Jul 07 '22 at 12:00
0

you should write full path with double slash like:

C:\\Users\\user name\\jupyter\\circle\\rect\\images

Oddaspa
  • 731
  • 5
  • 21
Khadhami
  • 1
  • 1