I tried to create an exe file, which is able to create json files and store it inside itself. The code of this minimal program looks like this:
import json
from os import listdir
from os.path import isfile, join
import os
from pathlib import Path
basedir = Path(__file__).parent.absolute()
def files_in_directory(path):
"""find all files in directory"""
return [f for f in listdir(path) if isfile(join(path, f))]
def save_json(data):
file = os.path.join(basedir, "data") + "/" + data + ".json"
with open(file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, ensure_ascii=False, sort_keys=False)
while True:
user_input = input("Input:")
if user_input == "stop":
break
elif user_input == "add":
number_input = input("Data:")
save_json(number_input)
elif user_input == "show":
print(files_in_directory(os.path.join(basedir, "data")))
else:
print("User input unkown")
The json files are stored inside the data folder, which is in the same directory as the python file. I created the exe file with pyinstaller
using the following spec-file
. The data folder is included in the program.
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['test.py'],
pathex=[],
binaries=[],
datas=[("data", "data")],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='test',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)
app = BUNDLE(
exe,
name='tesst.app',
icon=None,
bundle_identifier=None,
)
When I run the program, I can store and load the names of all data, which has been stored in this setting. But when I close the program and start it again, all data is gone. Is there a way, which keeps the data stored inside the exe file after closing the file?