简体   繁体   中英

How do I read json content from python file that I have made into an executable (Mac)

I recently compiled my first python program into a standalone executable. There is one problem however. My python program originally accessed a .json file within the same directory and wrote contents to it. Now that I have made my python file into an exec file, it does not seem to access the .json file in the same directory anymore. Please help!

I guess the problem occurs because the the executable is not in the same folder as your script. I can suggest you this solution:

import os

# csfp - current script folder path
csfp = os.path.dirname(os.path.realpath(__file__))
json_file = os.path.join(csfp, "config.json")

And you do whatever you want to do with this json_file variable, that contains the path and the name of your json file (example C:\\Some_folder\\My_python_project\\config.json)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM