Python脚本进行文件的模糊查询
1、模糊匹配进行搜索 文件名中包含“PRe”并且后缀是以“.zip"结尾
使用os库
使用endswith匹配字符串的末尾
代码如下:
import os
# 模糊匹配进行搜索 文件名中包含“PRe”并且后缀是以“.zip"结尾
path = "F:/python_code"
files = os.listdir(path)
for f in files:
if "PRe" in f and f.endswith(".zip"):
print("found it!"+f)
2、查询文件名包含abc或cd的文件,且后缀不能是gif
import os
path = "F:/python_code"
files = os.listdir(path)
for f in files:
if ("abc" in f or "cd" in f) and (not f.endswith(".gif")):
print("found it!" + f)