I am working on a python project that depends on some other files. It all works fine while testing. However, I want the program to run on start up. The working directory for programs that run on start up seems to be C:Windows\system32
. When installing a program, it usually asks where to install it and no matter where you put it, if it runs on start up, it knows where its files are located. How do they achieve that? Also, how to achieve the same thing in python?

- 3
- 1
- 2
-
I guess how the file system is mounted. I can get the python interpreter to run a script on boot the issue is that it doesn't know where its other files are. – legendary device Jan 27 '22 at 17:52
-
When I am testing the code, the working directory is whatever folder it is in. For example "C:\Users\Legendary Device\Desktop\Project". This is where all its files are so in python all I have to do is give it the file name. But when it runs on start up the working directory changes and it can no longer find these files. I can hard code the location of the files but I don't want that. I want to give the use the ability put the files wherever he/she wants. – legendary device Jan 27 '22 at 17:56
2 Answers
If you plan to consume local files that contain raw data or processed data, defining a default directory or a set of directories can simplify your implementation, for example:
Place your data files under a specific set of folders in C:\ or place your files under the F:\ folder, that can be a part of your on premisses file system
Based on where your Python application is located, you'll need to use relative paths or a library to help you to locate these files.
Here are some examples:

- 64
- 3
-
I don't want to define a set of available folders to use. I want whoever uses my program to be able to put the files wherever he/she wants. – legendary device Jan 27 '22 at 18:03
-
So I think that a possibility it's to make the program to ask the user what is the desirable path of her/his files. So she/he can pass this location via parameter to your program. – Ygor Rolim Jan 27 '22 at 18:11
-
That's what I thought but how to store the answer? When the program first runs it would ask the user where the files are, but then where would it store that answer so that it checks the provided folder every time instead of asking the user every time on start up? – legendary device Jan 27 '22 at 18:14
-
I would ask the user where her/his files are and also a second parameter about where she/he plan to place the answers. You could make this second parameter optional. If this parameter didn't set, you could use a default location to place the answers and then let the user know this default location. for example: by using that library called ```click```(https://click.palletsprojects.com/en/8.0.x/), let's supose that you'll run a python program with these two parameters: ```python3 your_program.py --input C:\source_provided_path\ --output C:\destination_optional_path``` – Ygor Rolim Jan 27 '22 at 18:22
-
Sorry I don't understand your solution. Using that, how will the .py file remember where the answer is when it is launched next time? Also, I don't want it to use a command prompt interface. I can make the GUI for it to ask the user to select the folder that he/she wants. The problem still is how will the .py file remember that answer? – legendary device Jan 27 '22 at 18:28
-
If it's not essential that the saved file be permanent, you can get the environmental variable TEMP and save the file location variable in the user's temp directory – Jim Robinson Jan 27 '22 at 18:29
-
Can you guide me through this or give me a link to that explains that? I never dealt with environmental variables before. – legendary device Jan 27 '22 at 19:01
First of all, what do you mean by "their files"? Windows applications can store "their files" in multiple places (including but not limited to %CommonProgramFiles%, %ProgramData% and %AppData%).
That being said, the common location for simple applications and scripts is to use the same directory as the .exe (or script).
In Python there seems to be multiple ways to find this path, this seems to work nicely:
import os
print(os.path.abspath(os.path.dirname(__file__)))
See also:

- 97,548
- 12
- 110
- 164
-
What I mean by "their files" is assets, user data etc. For example if I installed a game, it creates files like settings, game objects etc. How does it know where to look for them? While yes the .exe file being executed is in the same directory where all the other files are but I am assuming it has a different working directory. Meaning that if it is in `C:\Users\Legendary Device\AppData\Local\Programs\`, its working directory will instead be `C:Windows\system32`. So how does it know that its files are located in in AppData and not in its working directory? – legendary device Jan 28 '22 at 09:08
-
Thanks, all I needed to do was change the working directory to where the script is. This post helped me out to understand too. https://stackoverflow.com/questions/1432924/python-change-the-scripts-working-directory-to-the-scripts-own-directory – legendary device Jan 28 '22 at 09:55