|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import os
def search_file(start_dir, target) :
os.chdir(start_dir)
real_file = os.path.splitext(target)[0]
for each_file in os.listdir(os.curdir) :
for each in os.path.splitext(each_file)[0]:
if each == real_file :
print(os.getcwd() + os.sep + each_file) # 使用os.sep是程序更标准
if os.path.isdir(each_file) :
search_file(each_file, target) # 递归调用
os.chdir(os.pardir) # 递归调用后切记返回上一层目录
start_dir = input('请输入待查找的初始目录:')
target = input('请输入需要查找的目标文件:')
search_file(start_dir, target)
我把这个改成不包含后缀,只搜索文件名,用时会特别长,应该怎么改呢
改成这样就好了:
- import os
- def search_file(start_dir, target):
- os.chdir(start_dir)
- # real_file = os.path.splitext(target)[0] # 改成不要后缀的这个代码就可以不用了
- for each_file in os.listdir(os.curdir):
- each = os.path.splitext(each_file)[0]
- if each == target:
- print(os.getcwd() + os.sep + each_file) # 使用os.sep是程序更标准
- if os.path.isdir(each_file):
- search_file(each_file, target) # 递归调用
- os.chdir(os.pardir) # 递归调用后切记返回上一层目录
- start_dir = input('请输入待查找的初始目录:')
- target = input('请输入需要查找的目标文件:')
- search_file(start_dir, target)
复制代码
|
|