Reputation: 13
I am a high school student building a video game for a project, and you'll have to excuse me, I am very new to python/coding in general. I assumed this would be a relatively easy thing to figure out or find online but I haven't had any luck. Obviously with a video game there are assets and files that need to be used. How can I set the directory to something that will work if run on another computer. More details below:
Right now I have it set to os.chdir(r'C:\Users\User\Desktop\VideoGame'), and obviously if run on another computer the path would be different. How can I set it so it find that "VideoGame" file and then sets that as the directory.
Obviously I need a directory so python can find all the files I need. But let's say i take my folder with my code and all my assets on it and put it on a USB stick, then give it to my teacher, how can I be sure that the python he is running can find and use the files in that folder.
Upvotes: 0
Views: 615
Reputation: 1379
After you clarified, what I understood is that you want to load all your game assets in a removable drive. I am assuming you will be having the assets
folder in the directory where the main.py
file will be present.
So my approach to the problem is to first obtain the current working directory of the main.py
file and then add assets\
to it and then check if that directory actually exists or not. If yes, then carry on with the operation, else display an error message.
My solution:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = os.path.join(dir_path, r"assets")
if(os.path.exists(dir_path)):
# block of code
print()
else:
print("assets not found!\ncheck your directory")
If I am again missing out something, fell free to comment.
Upvotes: 1