Python 检查是否存在具有给定模板的文件

Python 检查是否存在具有给定模板的文件,python,Python,我需要执行一个循环,以便在运行期间检查是否将具有给定模板的文件添加到目录中 在伪代码中: template = "START_*_hello_*.pdf" while true: while "file having template does not exist": time.sleep(1) found_file = get_existing_file file_processing(found_file) os.path.ex

我需要执行一个循环,以便在运行期间检查是否将具有给定模板的文件添加到目录中

在伪代码中:

template = "START_*_hello_*.pdf"
while true:
      while "file having template does not exist":
            time.sleep(1)

      found_file = get_existing_file
      file_processing(found_file)
os.path.exists(文件路径)函数需要完整的文件名。如何使用包含*jolly字符的文件名


谢谢

使用
glob
模块,您可以编写如下内容:

import glob

template = "START_*_hello_*.pdf"

while True:
    files = glob.glob(template)
    if not files:
        # no file matching template exists. Try again later.
        time.sleep(1)
        continue
    # Process all existing files
    for file in files:
        file_processing(file)

听起来你是在找我想要的东西。我想你错过了一段时间。第一个while表示“永远”,第二个while表示“直到找到一个文件”@Fab Ah,因此您希望连续处理所有现有文件(即使是您已经处理的文件)?是的,随着时间的推移,可能会将具有相同模板的多个文件推送到目录中。已处理的文件将被删除deleted@Fab好了,不需要额外的循环。