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.
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.
Related
I am creating a program in Python that requires the user to place images into an Input folder, and then take images out of an Output folder. As this will become an application, the Input and Output folders will be very difficult to navigate to, being buried in the app's contents.
I am looking for a way to open folders onscreen so that a user can add or remove their own files from these folders, without knowing the exact location of the folders they are interacting with.
I am thoroughly stumped on this problem, and I appreciate all of your time.
Thank you very much.
*Edit: I am working on MacOS
If the program is intended for use on windows, it seems like you could use the solution here, where you could open explorer as a sub process and then open the path to the file.
For example
import subprocess
subprocess.Popen(r'explorer /select, FilePath')
You could also use os, and os.startfile(FilePath).
I'm writing a post-job script for Thinkbox Deadline. I need the directory for the rendered frames from a finished Cinema 4D job.
In Cinema, we had the frames know to go up a few directories, then back down to a different location by using this syntax "path\to\c4d_file..\..\renders\frame_00.png"
In this example the "\.." tells Cinema to go up 2 folders to the "path" folder then back down into the "renders" folder where it saves the renders to.
Unfortunately Deadline doesn't calculate the actual path when it makes the render job. When I tell the post-job script to go to the path location, it doesn't understand the '\..' -- so I need to tell it where to go up a directory and where to go back down.
Is there any sort of Python magic that can basically read the "\.." and know how to make the correct directory?
Thanks
Take the absolute path and remove the last one
path.sep.join(path.abspath('.').split(path.sep)[:-1])
I'm using inotify_simple to get notifications from a directory of directories. I'm accessing a directory that has multiple sub-directories, looping through those sub-directories and wanting to use inotify within each directory. My goal for this program is to be notified anytime anything happens (in particular, a creation of a file) in one of the sub-directories.
My file structure:
-mainDir
-subDir1
-file1
-file2
-subDir2
-file3
-file4
...etc.
I'm looping through the directories in mainDir, and setting that path as the path for inotify to search in:
for directory in os.listdir(self.path):
new_path = os.path.join(self.path, directory)
new_curr = self.inotify.new_curr_file_notification(new_path)
New path values are exactly what I expect:
.../mainDir/subDir1
.../mainDir/subDir2
When passing in new_path into my function (which is the path to give inotify), I'm expecting inotify to only look in that directory. However, I'm getting notifications that files in other directories are causing the notification.
path for inotify .../mainDir/subDir1
Event(wd=1, mask=256, cookie=0, name='someFileInSubDir2')
flags.CREATE
Does anyone know why this is happening? And, if anyone has any suggestions to make this process easier/better, I'm all ears! Thanks!
I'm the author of inotify_simple, and since it doesn't have a method called new_curr_file_notification, I'm guessing that's that's something you wrote. Without seeing that method, or some more code that demonstrates how you're using the library exactly, I unfortunately can't give you any advice, as there's not enough information to see how you're using inotify_simple.
If you post a complete example, I will probably be able to tell what's going wrong.
Feel free to post a bug report on the project't github if it looks like there might be a bug.
I am envolved in a project and i want to make a system that the user inputs a string like "summer_photos" and then the python searchs in all the folders and files and subfolders all the way down the C: tree and returns the absolute path of the files containing the name inputed. If i input summer_photos it can return
C:/User/Images/Summer1990/summer_photos.zip
C:/User/Images/Summer1991/summer_photos.zip
and etc.
As i am a petty beginner in Python using Windows i thought asking the masters of programming arts in stock overflow would be helpful.
You can achieve it by using os.walk() function
for path, dirs, files in os.walk("C:/"):
for name in files:
if "summer_photos" in name:
//add path to list
The walk function in the os module might be a good place to start. And keep in mind that checking every file name in the entire hard drive will take forever; consider limiting the search to a user-specified directory :-)
This is my first question.
My python script opens and reads from a present text file using the following simple funct:
open("config.ini", "r")
As this is a relative path it is supposed to work because config.ini is placed in the same directory like the script is when it is launched, that should be the current working dir.
In fact this works perfectly on all of my 3 linux boxes, but I have one user who demands support because he gets an error while opening config.ini. The error raises because
os.path.exists("config.ini")
returns false even if the file is there!
Trying to fix this problem we found out that the only way to make it work is to place config.ini in his home directory despite the supposed working directory is another.
Also, if my script tries to create a file in the present working directory, the file is always created in his home dir instead, and so I think that for some reason his working dir is always home!
How can I troubleshoot this problem? Maybe I could introduce absolute paths, but I am afraid that os.getcwd() would return the homedir instead of the correct one.
Should I maybe suggest this user to fix his machine in some way?
Sorry for this long question but english is not my first language and I am a beginner in coding, so have some difficulties to explain.
Thank you very much in advance! =)
Could it be that the user is executing your script from his home directory?
I.e. suppose the script is in:
/home/user/test/foo/foo.py
But the user calls it thus:
/home/user> python test/foo/foo.py
In this case, the "current directory" the script sees is /home/user.
What you can do is find out the directory the script itself resides in by calling this function:
import os
def script_dir():
return os.path.dirname(os.path.realpath(__file__))
It will always return the directory in which the script lives, not the current directory which may be different. You can then store your configuration file there safely.
Along the same lines as Eli Bendersky's suggestion, you might want to try:
os.path.exists(os.path.join(sys.path[0],"config.ini"))
since sys.path[0] should always be the directory in which the script resides.