在python中从部分文件名中查找文件

Gab*_*lf1 0 python directory search filenames file

我正在寻找一个 python 脚本,它将在当前目录中找到一个现有文件的确切文件名,该文件名将运行该 python 脚本,可以增量命名。

例如,文件可能是: file1.dat file2.dat file3.dat ....

所以我们知道文件名以前缀开头,file我们知道它以 sufix 结尾.dat

但是,我们不知道它是否会file1.dat还是file1000.dat什么的插图中。

所以我需要一个脚本来检查1-1000file1.dat到的所有文件名的范围file1000.dat,如果它找到目录中确实存在的文件名,它会返回一条成功消息。

小智 5

尝试这样的事情:

import os

path = os.path.dirname(os.path.realpath(__file__))

for f_name in os.listdir(path):
    if f_name.startswith('file') and f_name.endswith('.dat'):
        print('found a match')
Run Code Online (Sandbox Code Playgroud)