如何在Python的子目录中找到文件?

时间:2018-12-05 17:35:36

标签: python

例如函数应返回:

  • stackpath =“ config / openvpn / openvpn02.json”

对于stackname =“ openvpn02”。

如何在配置目录中搜索“ openvpn02.json”?

1 个答案:

答案 0 :(得分:1)

像这样

import os

to_search = "filename"

for dirpath, dirnames, filenames in os.walk("."):
    for filename in [f for f in filenames if "".join(f.split(".")[:-1])==to_search]:
        print os.path.join(dirpath, filename)