0

I'm new and I'm trying to run a script that runs a sequence of scripts based on what I put in a text file.

For example, I have Script1.py with print("Script1") ... then Script2.py with print("Script2"), Script3.py with print("Script3") ... and in the text file to once:

Script1.py
Script3.py

then another time to have:

Script2.py
Script1.py
Script3.py
Script2.py

And the main script to execute the sequence of scripts in the text file.

PS: Apologies for my English language

Thank you in advance

def Script1():
    Script1.py
def Script2():
    Script2.py
def Script3():
    Script3.py

commands_table = {'Script1':Script1, 'Script2':Script2, 'Script3':Script3}

with open('test.txt', 'r') as command_file:
     for cmd in command_file:
         cmd = cmd.strip()
         commands_table[cmd]()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Does this answer your question? [What is the best way to call a script from another script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script) – Random Davis Feb 15 '23 at 18:27
  • "I'm trying to run a script that runs a sequence of scripts based on what I put in a text file." So the file specifies the name of a python file or the name of a python function? – erik258 Feb 15 '23 at 18:27
  • Yes. The file specifies the name of a python files to run – Adrian Serbanescu Feb 15 '23 at 18:29
  • This is a strange way to do things. Why not just pass the names of the features that you want run on the command line (e.g. main.py Script1 Script2) and then the main script imports and executes the relevant Python source? – jarmod Feb 15 '23 at 18:34

2 Answers2

0

Use import to grab functions from other files

scripts.py

def script1():
  print('1')

def script2():
  print('2')

app.py

import scripts

commands_table = {'Script1':scripts.script1, 'Script2':scripts.script2}

with open('test.txt') as command_file:
     for cmd in command_file:
         commands_table[cmd.strip()]()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0
import os

with open('test.txt', 'r') as command_file:
    commands = command_file.read()
    commandsList = commands.splitlines()
    for cmd in commandsList:
        os.system(cmd)

You have to chmod +x all your scripts to be executable and the first line of each script should be:

#! /usr/local/bin/python3

or whatever your python interpreter is

Tourelou
  • 123
  • 4