So I have this pretty simple line of code that opens up a .exe file that gets put inside the subprocess.call bracket. However, I always need to specify in full length the location of the .exe file that I'm trying to run. How do I make it so that I don't have to specify it by putting the .py file with the code below inside the same directory with the .exe file that I'm running?
import subprocess
def openHC():
try:
subprocess.call("R:/Codes/crackingAtHome/crackingAtHomeClient/hashcat.exe")
except:
print("something wrong happened")
openHC()
I tried to put the .py file in the same directory with no success. I still have to fully specify it for it to run.
When python runs a script, it usually includes the __file__
variable, naming the .py file being run. You can use that name to find other files in the same directory. This is usually an absolute path, but I don't think its required to be so, so its a good idea to make it absolute anyway. The pathlib
provides a convenient way to do the calculations.
import subprocess
from pathlib import Path
try:
parent_path = Path(__file__).absolute().parent
except:
# parent path unknown, hope cwd works
parent_path = Path(".")
def openHC():
try:
subprocess.call(f'{parent_path/"hashcat.exe"} --help',
shell=True)
except:
print("something wrong happened")
openHC()