I have developed an app that is suppose to read a text file from the same directory of the script being run. In Visual Studio, the following code works no issues and responds with the expected folder:
WorkingDirectory = os.path.dirname(os.path.abspath(__file__))
Testing101 = os.chdir(WorkingDirectory)
print("Working Directory is: "+ str(WorkingDirectory))
print("Changing Directory to: "+ str(WorkingDirectory))
However when I convert this to a .exe using pyinstaller FileName.py --onefile
in the visual studio terminal then run the .exe it generates, the code above points to a completely different folder than where the script is being run from.
Example (not actual folder paths) Folder 1 - script location "D:/Python Testing" Folder 2 - random folder "C:/user/name/temp"
I have attempted to take hints from (How do I get the full path of the current file's directory?) and other sources but none seem to redirect the script to the expected path.
Below is the part of my code that i am having the issues with:
print("Loading... \nStandby...")
#==============================================
# Notes
#==============================================
'''
Version Notes:
Beta 0.1: GUI Layout
Basic Functionality
'''
#==============================================
# Libraries
#==============================================
print("Importing Libraries...")
import os
import pathlib
#Other Libraries
print("Libraries Imported...")
#==============================================
# Global Variables
#==============================================
(Not used yet)
#==============================================
# Change Working Directory
#==============================================
WorkingDirectory = os.path.dirname(os.path.abspath(__file__))
Testing101 = os.chdir(WorkingDirectory)
print("Working Directory is: "+ str(WorkingDirectory))
print("Changing Directory to: "+ str(WorkingDirectory))
I have tried following numerous YouTube videos, google searches and stack overflow examples - none worked at redirecting the script to the correct folder.