0

I have generated an .exe from the script using auto-py-to-exe and it works correctly. The problem comes when it comes to get routes. When I run the program I get this:

Script located at:
C:\Users\username\AppData\Local\Temp\_MEI26762
Bases are taken from:
C:\Users\username\AppData\Local\Temp\_MEI26762/base/
Logs are stored in:
C:\Users\username\AppData\Local\Temp\_MEI26762/logs/

The .exe is located in C:\Users\username\Desktop\Bot so obviously the path for the folders "base" and "logs" are incorrect. I need to obtain:

Script located at:
C:\Users\username\Desktop\Bot
Bases are taken from:
C:\Users\username\Desktop\Bot/base/
Logs are stored in:
C:\Users\username\Desktop\Bot/logs/

This is my python code:

# GET THE CURRENT LOCATION OF THE SCRIPT
script_dir = os.path.dirname(os.path.abspath(__file__))
print('Script located at:')
print(script_dir)

# CHANGE THE WORKING DIRECTORY TO THE CURRENT LOCATION OF THE SCRIPT
os.chdir(script_dir)

new_cwd = os.getcwd()

BASE_FOLDER = new_cwd + '/base/'
LOG_FOLDER = new_cwd + '/logs/'

print('Bases are taken from:')
print(BASE_FOLDER)
print('Logs are stored in:')
print(LOG_FOLDER)

base_csv = os.listdir(BASE_FOLDER)
  • Read carefully [the relevant pyinstaller docs](https://pyinstaller.org/en/stable/runtime-information.html). The output you get is correct. – buran Mar 14 '23 at 12:14
  • You know it is correct or as stated in the question _obviously the path for the folders "base" and "logs" **are incorrect.**_? There is a small code snippet in the docs that gives you ANY possible path. Did you try to run it and understand the different paths? `sys.executable` will give you the path of the exe file – buran Mar 14 '23 at 12:32

1 Answers1

0

Solved! I just needed to change:

script_dir = os.path.dirname(os.path.abspath(__file__))
print('Script located at:')
print(script_dir)

to:

exe_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
print('Executable located at:')
print(exe_dir)