Marie
Marie

Reputation: 146

Incorrect path in Python Script when called from remote subprocess

I have two scripts : main.py and worker.py

If I execute my worker.py, which is just reading the parameters.txt file, using the path 'data/parameters.txt'. It works great.

If I call the worker.py from the main using

command = ['python', '../worker/worker.py']
subprocess.call(command)

I got an error.

  File "../main/main.py", line 11, in getTxt
input_file = open(file_name, 'r')
 FileNotFoundError: [Errno 2] No such file or directory: '/data/parameters.txt'

I feel like when I call the subprocess from the main.py, it is not creating a instance of the py script, like when I'm executing 'python worker.py' from worker folder, so it mixes the path. For exemple, I feel like it's not looking for the parameters.txt in the worker folder, but in the main folder.

How to solve this ? Knowing my worker.py is 100% independent from main.py talking about variables and arguments. But need to be call from main.py

This is different than importing or exec the worker.py. I want to execute multiple worker.py at the same time and they automaticaly close themselves when they are done.

Many thanks

Upvotes: 1

Views: 774

Answers (2)

Cow
Cow

Reputation: 3040

subprocess has a cwd variable you can tell it where to execute the script from. This is how I solved your problem:

import subprocess
from os import getcwd, chdir
from os.path import join

CURRENT_DIR: str = getcwd()
chdir(CURRENT_DIR)
LOCATION = join(CURRENT_DIR, "worker")

command = ["python", "worker.py"]
subprocess.call(command, cwd=LOCATION)

worker.py - for testing purpose:

with open("parameters/test.txt") as f:
    print(f.read())

Result:

hello!

This was the contents of test.txt for testing.

Upvotes: 1

AKMalkadi
AKMalkadi

Reputation: 982

You may need to change the current working directory

Get current path:

import os
current_path = os.getcwd()

Change the current working directory

os.chdir('../worker')

Run your process:

command = ['python', 'worker.py']
subprocess.call(command)

Finally get back to your previous path

os.chdir(current_path)

Upvotes: 1

Related Questions