2

I would like to use relative paths along with subprocess module in order to be able to run different executables.

For getting relative paths and, after reading different threads, I think pathlib module is the best option to do that.

Let assume I have the python script in a particular folder in Windows. Inside it (the previous folder), I have other folders with the executables I want to run. Here it is when subprocess module comes in. However, I do not know how to include the relative paths created with pathlib module into subprocess args field.

From subprocess API I can read that 'args should be a sequence of program arguments or else a single string'.

import pathlib
import subprocess

in_file_1 = pathlib.Path.cwd() / "folder2" / "folder3" / "whatever.exe"

p = subprocess.Popen(str(in_file_1), shell = True)

I would expect to see the whatever.exe process running on the administrator tasks but the process is not started. How can I achieve that? Is there something I am ingoring? Should I just give the relative path from where the python script is saved?

4
  • 3
    I just tested your snippet and everything works as expected. Are you sure about your whatever.exe location? Why do you want to use the current working dir? Commented Jan 30, 2019 at 10:32
  • Yes, I am sure about the whatever.exe location. If I save the python script in the same folder as the whatever.exe the above code works but If I save it upstream whatever.exe does not work even specifying the relative path to it. Commented Jan 30, 2019 at 11:32
  • You are using pathlib.Path.cwd() so what matters is the current dir when you launch the script, not where the script is located. Commented Jan 30, 2019 at 11:53
  • That is what I am trying to do with ´in_file_1´ in order to obtain the current location of the python script. But even with that, I cannot make it work. Thank you for your comments @nicoco Commented Jan 30, 2019 at 12:06

1 Answer 1

4

You are confusing the current working directory, which is what pathlib.Path.cwd() returns, with the script's location.

If you want the script's dir, you can use __file__, for instance like this:

import pathlib

cwd = pathlib.Path.cwd()
script_file = pathlib.Path(__file__)
script_location = script_file.parent

print("The current dir is", pathlib.Path.cwd())
print("The current script is", script_file)
print("The current script's dir is", script_file.parent)

which will return:

The current dir is /home/nicoco
The current script is /tmp/so.py
The current script's dir is /tmp
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help @nicoco. Now it is working perfectly. I did not realize about the script's location! I have just put ´whatever.exe´ as arg for ´subprocess.Popen´ and specified the path with cwd argument. Thank you once again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.