Skip to content Skip to sidebar Skip to footer

Filenotfounderror: [errno 2] No Such File Or Directory: 'classa' In Python, Although The File Has Already Been Made

sav = [] def fileKeep(sav): classA = open('classA', 'r') for line in classA: sav.append(line.split()) file.close() return fileKeep(sav) This is the end of

Solution 1:

You code is assuming that the current working directory is the same as the directory your script lives in. It is not an assumption you can make.

Use an absolute path for your data file. You can base it on the absolute path of your script:

import os.path

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
class_a_path = os.path.join(BASE_DIR, "classA")

classA = open(class_a_path)

You can verify what the current working directory is with os.getcwd() if you want to figure out where instead you are trying to open your data file.

Your function could be simplified to:

deffileKeep(sav):
    withopen(class_a_path) as class_:
        sav.extend(l.split() for l in class_)

provided class_a_path is a global.

Post a Comment for "Filenotfounderror: [errno 2] No Such File Or Directory: 'classa' In Python, Although The File Has Already Been Made"