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:ProjectsPyEngineTestProjectmain.py", line 12, in <module>
objects = load_data("objects.pkl")
^^^^^^^^^^^^^^^^^^^^^^^^
File "d:ProjectsPyEngineTestProjectengineFunctions.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 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 = load_data("data.pkl")
print(data.name)
The 2 scripts run separately
the full code link is -> https://github.com/IGR2020/PyEngine