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

坚强是说给别人听的谎言 提交于 2019-12-13 07:23:42

问题


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`


回答1:


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



回答2:


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"])
......


来源:https://stackoverflow.com/questions/14679629/python-file-to-open-a-text-file-and-run-other-python-files-in-the-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!