'Opening apps without the directory (or with replacement of users folder) in Python
So, I want to make a python program which can locate and open Discord & Minecraft and open it on other computers. And itself it's pretty easy to do if you have a directory in which the files are located, but my problem is I want to share this file with other people, and I want it to work on their PC too. This is my code:
import subprocess
subprocess.run("C:\\Users\\1\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe")
subprocess.run("E:\Geim\Minecraft Launcher\MinecraftLauncher.exe")
My Minecraft location is sligtly weird cause I need more memory so look only only on the discord example, if the solution would be replacing the users folder I'll deal with that
So the problem with this code again is that it works only on my computer since my users directory is named "1"
I also tried using os and retrieving the name which windows identifies Minecraft using Winapps module ("Minecraft Launcher") and while it works for chrome os can't read spaces and replacing it with "-" or "_", or simply lowercasing the letters didn't work
import os
os.system("minecraft launcher") # doesn't work
I also found this intersting chunk of code but I couldn't implement it in directory in the subprocess
from pathlib import Path
home = str(Path.home())
subprocess.run("C:\\Users\\" + home + "\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe") # doesn't work
It seems to be an easy problem for more skilled programmers so I hope to see help soon, thank you.
Edit: I was able to solve the problem by using pathlib and replacing "/" with "\", however I'd still like to receive an answer where it can be found in any not set directory (like mine "E\Geim...) Here's my working code:
import subprocess
from pathlib import Path
home = str(Path.home())
home = home.replace("\\", "/")
subprocess.run(home + "\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe")
subprocess.run(home + "\\AppData\\Roaming\\.minecraft\\TLauncher.exe")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
