Skip to content
Advertisement

How to run python scripts in another directory, without writing scripts to disk?

Suppose I have two python scripts methods.py and driver.py. methods.py has all the methods defined in it, and driver.py does the required job when I run it.

Let’s say I am in a main directory with the two files driver.py and methods.py, and I have n subdirectories in the main directory named subdir1,subdir2,…,subdirn. All of these subdirectories have files which act as inputs to driver.py.

What I want to do is run driver.py in all these subdirectories and get my output from them, without writing driver.py to disk.

How should I go about this? At the moment, I am using the subprocess module to

  1. Copy driver.py and methods.py to the subdirectories.
  2. Run them.

The copying part is simple:

import subprocess

for i in range(n):
  cmd = "cp methods.py driver.py subdir"+str(i)
  p = subprocess.Popen(cmd, shell=True)
  p.wait()

#once every subdirectory has driver.py and methods.py, start running these codes 

for i in range(n):
  cmd = "cd subdir" + str(i) +" && python driver.py"
  p = subprocess.Popen(cmd, shell=True)
  p.wait()

Is there a way to do the above without using up disk space?

Advertisement

Answer

you might use pythons os.chdir() function to change the current working directory:

import os
#import methods

root = os.getcwd()

for d in ['subdir1', 'subdir2']:
  os.chdir(os.path.join(root, d))
  print("dir:", os.getcwd())
  exec(open("../driver.py").read())

I am also not sure if you need popen, since python is able to execute python files using the exec function. In this case it depends on how you import the methods.py. Do you simply import it or use it somehow else inside of your driver.py ?
You could try to import it at toplevel inside your main script or use an extended path like:

exec(open("../methods.py").read())

inside of your driver script. Keep in mind these solutions are all not very elegant. Best would be processing the path inside of your driver.py as suggested by Gino Mempin. You could call os.chdir() from there.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement