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).
Related
I am wondering if there is an easy way to access 'parallel' directories (See photo for what I am talking about... I don't know what else to call them, please correct me if they are called something else!) from a Python file without having to input the string path.
The basic structure I intend to use is shown in the picture. The structure will be used across different computers, so I need to avoid just typing in "C:\stuff_to_get_there\parent_directory\data\file.txt" because "C:\stuff_to_get_there" will not be the same on different computers.
I want to store the .py files in their own directory, then access the data files in data directory, and save figures to figures directory. I was thinking of trying os module but not sure if that's the correct way to go.
parent directory
scripts
.py files
figures
save files here
data
.txt files stored here
Thanks for any help!
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.
I am having problems with Project Multiclipboard from Chapter 8 of the book: Automate the Boring Stuff and using Python 3.
The first issue is that, suppose my program mcb.pyw is saved in:
C:\Users\myName\folder name
where the last folder has a space in the name, my batch file:
#pyw.exe C:\Users\myName\folder name\mcb.pyw %*
doesn't seem to work properly from the command line. I can now type in
mcb save keyword
into the command line without getting an error, but it's not doing anything. After testing by changing the directory to a folder whose path has no space in it, I've concluded that the problem is because of the space, but I am unsure of how I might go about fixing this.
The second issue is that when the batch file is working, the module shelve seems to be saving the data in the wrong folder. Specifically, I noticed that if I were to run mcb.pyw from the command line, shelve would save the data in C:\Users\myName, which is also the default directory when you open the command windod, instead of the folder C:\Users\myName\folderName, where mcb.pyw and mcb.bat are saved.
I have gotten around this by including the lines:
import os
os.chdir('C:\\Users\\myName\\folderName')
However, is there any other way to solve this issue? Why is shelve saving in C:\Users\myName instead of the folder where everything is already saved?
I apologise if I have made any ettiquette or formatting problems. If you let me know what I did wrong I will do my best to fix it as soon as I can, thank you!
Files are always saved in the current working directory unless they are specified with path names, so you do have to change your working directory if the default one is not what you want.
You can avoid hard-coding the path name and always change your working directory where the script is located with:
import os
import sys
os.chdir(os.path.dirname(sys.argv[0]))
I have a directory of .xml files, and one exe file.
I would like to write a python script which will use those .xml files (one at a time) on the .exe file emulating dragging and dropping. It cannot however actually control the mouse cursor.
I've looked for resources on this but cannot find anything.
Any hints in the right direction would be very helpful, if it is even do-able.
Thank you for your time.
Forget about controlling the cursor: Dragging and dropping on Windows simply calls the program you drop things on, with the full path of the dropped files as arguments. So if you want to run myprogram.exe on all files in its folder, do something like this:
from glob import glob
import subprocess
for filename in glob("./*.xml"):
subprocess.call(["myprogram.exe", filename])
You were not entirely clear on what you wanted to do, so you may have to make some adjustments, but it should be along such lines. The call to glob is to ensure that only xml files get run.
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.