python 2.7 扫描指定目录下的文件是否包含 html 文件,并打印路径
时间: 2024-02-01 15:13:06 浏览: 22
可以使用 Python 的 os 模块和 fnmatch 模块来扫描指定目录下的文件并判断是否包含 html 文件,示例代码如下:
```python
import os
import fnmatch
root_path = "/path/to/directory"
for root, dirnames, filenames in os.walk(root_path):
for filename in fnmatch.filter(filenames, "*.html"):
file_path = os.path.join(root, filename)
print(file_path)
```
上述代码中,首先定义了要扫描的根目录 root_path,然后使用 os.walk 函数遍历该目录下的所有子目录和文件,并使用 fnmatch.filter 函数过滤出所有以 .html 结尾的文件名,最后将文件路径拼接起来并打印出来。