【问题标题】:How to call bunch of python executable files mentioned in .txt file from one script如何从一个脚本调用.txt文件中提到的一堆python可执行文件
【发布时间】:2013-02-01 20:04:27
【问题描述】:

我正在编写一个 python 脚本,例如startTesting.py 位于“C:\python\Trigger”文件夹下。该文件夹有一个 list.txt 文件,其中保存了可执行 python 文件的列表(在另一个文件夹下)。我在我的 startTesting.py 脚本中指向这个 list.txt 文件,以便 list.txt 中提到的所有测试文件都将被执行。

测试文件保存在另一个文件夹“C:\python\Executable”下

我的 python 代码没有访问 Executable 文件夹下的 python 脚本。

List.txt 看起来像:

../Executable/testscript1.py

../Executable/testscript2.py

我的 startTesting.py 看起来像:

>>> import os

>>> test_case_list = "List.txt"

>>> os.system('python %s' %test_case_list)

但是当试图执行文件时,我得到了这个错误:

File "List.txt", line 1

../Testware/testscript.py
^

欢迎任何解决这个问题的猜测!!!

【问题讨论】:

    标签: python-2.7


    【解决方案1】:

    如果要运行文件中包含的脚本,请阅读该文件:

    with open('List.txt', 'r') as handle:
        for line in handle:
            execfile(line.strip())
    

    你现在正在运行List.txt,它不会很好地工作。

    【讨论】:

    • 嗨,我试过并得到了这个错误:>>> with open('List.txt', 'r') as handle: ... for line in handle: ... execfile(line ) ... 回溯(最近一次调用最后一次):文件“”,第 3 行,在 IOError:[Errno 2] 没有这样的文件或目录:'../Executable/testscript.py\n' >>>
    • @user2033758:哎呀,对不起。忘了去掉换行符。立即尝试。
    • 我使它与您给出的示例一起工作,在这里它是如何工作的:test_case_list = 'list.txt' testfile = open(test_case_list,'r') lines = testfile.readlines() for line行: os.system('python %s' % line)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    相关资源
    最近更新 更多