Search code examples
pythonpython-2.7windows-shell

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`


Solution

  • 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])