python文件打开一个文本文件并运行文本文件中的其他python文件 [英] Python file to open a text file and run other python files in the text file

查看:68
本文介绍了python文件打开一个文本文件并运行文本文件中的其他python文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,里面有几个 python 文件的路径以及我在命令提示符下运行它们时指定的参数.

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.

我正在寻找一个 python 脚本,它打开文本文件并运行文本文件中指定的 python 程序以及提供的参数.

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.

文本文件看起来像`C:\hello.py 世界

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

C:\square.py 5`

C:\square.py 5`

推荐答案

我认为这篇文章不值得投反对票.但是从现在开始,我会建议 OP 自己寻找解决方案,然后在堆栈溢出时找不到答案!

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

这篇关于python文件打开一个文本文件并运行文本文件中的其他python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆