获得徽章 0
赞了这篇沸点
赞了这篇文章
赞了这篇文章
#码上掘金# import os
# 定义文件查找函数
def search_files(path, name=None, ext=None):
result = []
for root, dirs, files in os.walk(path):
for file in files:
if name and name not in file:
continue
if ext and not file.endswith(ext):
continue
result.append(os.path.join(root, file))
return result
# 在指定路径下查找指定名称和扩展名的文件
path = "/path/to/search"
name = "example"
ext = ".txt"
result = search_files(path, name=name, ext=ext)
if result:
print(f"Found {len(result)} files:")
print("-----------------------")
for file in result:
print(file)
else:
print("No files found.")#码上掘金#
# 定义文件查找函数
def search_files(path, name=None, ext=None):
result = []
for root, dirs, files in os.walk(path):
for file in files:
if name and name not in file:
continue
if ext and not file.endswith(ext):
continue
result.append(os.path.join(root, file))
return result
# 在指定路径下查找指定名称和扩展名的文件
path = "/path/to/search"
name = "example"
ext = ".txt"
result = search_files(path, name=name, ext=ext)
if result:
print(f"Found {len(result)} files:")
print("-----------------------")
for file in result:
print(file)
else:
print("No files found.")#码上掘金#
展开
1
点赞