pythonsavepickle

FileNotFoundError when being loading a file using a script in a subdirectory, with file in subdirectory. (Pickle)


I have a project in which a app loads and saves data into sub folders (its a GUI editor), and the script for loading the pickle file and using the data gives an error message ->

Traceback (most recent call last):
  File "d:\Projects\PyEngine\TestProject\main.py", line 12, in <module>
    objects = load_data("objects.pkl")
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Projects\PyEngine\TestProject\engineFunctions.py", line 12, in load_data
    with open(path, "rb") as file:
         ^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'objects.pkl'

The file structure is ->

Workspace Folder
    main.py
    other python files
    Sub Folder
        main.py
        other python files
        objects.pkl

The sub folder main.py file is able to load properly when subfolder is not in workspace folder, it does work if I also specify the sub folder name when giving the path, but then it doesn't work outside of the workspace folder.

Here is a MRE-

file structure ->

Workspace Folder
   main.py
   folder
      main.py

main.py ->

import pickle

class RandomObject:
    def __init__(self) -> None:
        self.name = "Random Object"


def save_data(data, path):
    with open(path, "wb") as file:
        pickle.dump(data, file)
        file.close()

def load_data(path):
    with open(path, "rb") as file:
        data = pickle.load(file)
        file.close()
    return data

data = RandomObject()
save_data(data, "folder/data.pkl")
data = load_data("folder/data.pkl")
print(data.name)

folder/main.py ->

import pickle

class RandomObject:
    def __init__(self) -> None:
        self.name = "Random Object"

def load_data(path):
    with open(path, "rb") as file:
        data = pickle.load(file)
        file.close()
    return data

data = load_data("data.pkl")
print(data.name)

The 2 scripts run separately the full code link is -> https://github.com/IGR2020/PyEngine


Solution

  • It is not where the main.py is located but where the python executable is run that will determine if it can access the file directly. This is called the working directory where the executable is run. In other words, if you are in the Terminal and you do (I presume that you are running in Windows):

    cd "d:\path\to\Workspace Folder"
    py main.py
    

    Inside your main.py, when you are doing the equivalent of open("data.pkl") it will look in d:\path\to\Workspace Folder for that file.

    If you do:

    cd "d:\path\to\Workspace Folder"
    py folder/main.py
    

    It will still look in d:\path\to\Workspace Folder for that file since this is location where the interpreter is launched.

    If you do:

    cd "d:\path\to\Workspace Folder\folder"
    py main.py
    

    This time, it will look in d:\path\to\Workspace Folder\folder for that file.


    But in your case since you are running in VSCode, you should customize a launch configuration so that you run the python executable in the right directory. I will refer you to this answer on how to do it.