Javier Lopez Tomas
Javier Lopez Tomas

Reputation: 2342

How to create a path for the argument cwd in subprocess.run

I have the following paths:

airflow_home = os.path.join("opt", "airflow")
airflow_docs = os.path.join(airflow_home, "docs")

And I want airflow_docs path to be used within a bash command. For that, I have used the following code:

subprocess.run([f"sphinx-apidoc -o ./ ../plugins"],
                       shell=True,
                       cwd=airflow_docs)

And I get an error FileNotFoundError.

However, this does work:

subprocess.run([f"sphinx-apidoc -o ./ ../{doc_module}"],
                       shell=True,
                       cwd="/opt/airflow/docs")

So it seems that a missing leading slash is causing the problem. I have searched in google about adding a leading slash to a path with no success. So, is it possible to use os.path package for subprocess.run, or do I have to use a hardcoded string?

Upvotes: 0

Views: 982

Answers (1)

tripleee
tripleee

Reputation: 189317

If you want a slash, put a slash.

airflow_home = os.path.join("/opt", "airflow")

But of course, having Python glue together the strings is not really useful. Indeed, the result of os.path.join is simply a string, equivalent to a hard-coded string. So just write it out:

airflow_home = "/opt/airflow"

Or if you want to do this in Python, perhaps prefer pathlib:

airflow_home = pathlib.Path("/opt") / "airflow"

As an aside, your subprocess code is broken; you want to pass either a string, with shell=True, or a list of tokens, without shell=True. (Windows "helpfully" hides this error but it's still wrong.)

subprocess.run(
    ["sphinx-apidoc", "-o", "./", "../plugins"],
    cwd=airflow_docs)

subprocess conveniently allows you to pass in a pathlib.Path object as the value of cwd, though this might not always have been the case, if you need to support older versions of Python.

You probably want to add check=True to have Python raise an error if the subprocess fails. Perhaps see also Running Bash commands in Python

Upvotes: 2

Related Questions