0

I have a program that I need to run in a list of folders. I have the file path to where the program is and the list to all of the folders where I would like to run the program on my computer (there's about 200 of them). I can also change the current working directory to get to the folder I want to be in.

How do you get python to execute a program through the actual IDE once you are in the folder you want to run the program in? I don't want to have to manually open a terminal on my computer, type "[Program name] [Path to file where I want to run the program]" 200+ times. The code I have is below

cat = '/home/myname/catalogue.csv'
cat = Table.read(cat, format="ascii")

ID = np.array(cat['ID'])
ID = ID.astype(str)

folder_path = np.array([])

for i in ID:
    folder_path = np.append(folder_path, '/home/myname/python_stuff/{}/'.format(i))

folder_path = folder_path[folder_path.argsort()]

for i in zip(folder_path, ID):
    os.chdir(i[0])
    name = i[1] +(".setup")

Essentially after the last line in my code I want another line that is the python equivalent of "run [program name] on name (which is the name of the file in each folder I want it to use)

  • Does this answer your question? [Run a Python script from another Python script, passing in arguments](https://stackoverflow.com/questions/3781851/run-a-python-script-from-another-python-script-passing-in-arguments) – Qiu Mar 12 '20 at 14:00
  • As an aside, why are you using numpy arrays here? – AMC Mar 12 '20 at 20:08

2 Answers2

0

You can run process as a subprocess with changing working directory

import subprocess
p = subprocess.Popen(["python","some_script.py"], stdout=subprocess.PIPE, cwd=PATH)
output = p.communicate()[0]

cwd means current working directory so you can run your script in another script on specific directory.

https://docs.python.org/3/library/subprocess.html#popen-constructor Check this guide to run subprocess with different directory

-1
import subprocess

for i in zip(folder_path, ID):
    os.chdir(i[0])
    name = i[1] +(".setup")
    subprocess.call("[path to program] %s" % (name) , shell=True)
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Igor F. Mar 13 '20 at 07:29