Kumaran Senapathy
Kumaran Senapathy

Reputation: 1283

Python file to open a text file and run other python files in the text file

I have a text file inside which I have paths to a few python files and the arguments that I would specify when I run them in a command prompt.

I am looking for a python script that opens up the text file and runs the python programs specified in the text file along with the provided arguments.

The text file will look something like `C:\hello.py world

C:\square.py 5`

Upvotes: 0

Views: 5497

Answers (2)

Greg
Greg

Reputation: 5588

I don't think this post deserves down voting. But from now on I would suggest to OP to look for a solution yourself, and then if you can't find the answer post on stack overflow!

from subprocess import call

with open("somefile.txt", 'r') as f:
    some_files_to_run = [line.split('\n')[0] for line in f.readlines()]
    for file_to_run in some_files_to_run:
        call(["python", file_to_run])

Upvotes: 1

Shawn Zhang
Shawn Zhang

Reputation: 1884

i think you should refer to below :

Calling an external command in Python

  1. Step One

read all lines in your command file get a list of python script file name and arguments like: " C:\hello.py and argument: word "

  1. Step Two

call them in below code style

from subprocess import call
call(["python C:\hello.py", "word"])
......

Upvotes: 2

Related Questions