迭代文件的Python脚本,使用通配符扩展,并通过args

问题描述 投票:0回答:2

我有一些脚本对文件进行一些处理。通常,他们接受该文件作为其第一个命令行参数,之后接受其他参数。

我想编写一个主脚本,它接受要运行的脚本的名称,一个指定目标文件的通配符(a-la glob),以及可能传递给输入脚本的参数。主脚本应遍历文件,并使用其他参数运行输入脚本。

请注意,输入脚本是遗留的,并且最后可能不包含通常的if __name__ == "__main__":行。他们也可以访问sys.argv

有什么建议?

python loops wildcard glob execfile
2个回答
0
投票

import glob

应该让你入手,

我也会对脚本进行子处理并传递参数。


0
投票

你可能会尝试这样的事情。 (非常粗糙,但你明白了)

import os
import sys

def callScripts(wildcard, arguments):
    # convert a list ["hello", "world"] to a space delimited "hello world"
    arguments = " ".join(arguments)
    for file in os.listdir(".")
    # feel free to replace with regex or w/e
    if file.endswith(wildcard)
        # system shell call
        os.system("python " + file + " " + arguments)

if __name__ == "__main__":
    wildcard = sys.argv[1]
    arguments = sys.argv[2:]
    callScripts(wildcard, arguments)
© www.soinside.com 2019 - 2024. All rights reserved.