|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
#查找目录及子目录下所有的匹配文件
import os
def Find_Name(FindName, FilePath):
for name in os.listdir(FilePath):
if os.path.isdir(name):
FilePath = os.path.join(FilePath_INT, name)
Find_Name(FindName, FilePath)
else:
if name == FindName:
print (os.path.join(FilePath, name))
FilePath_INT = r'C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\mine'
FindName = 'something.txt'
Find_Name(FindName, FilePath_INT)
运行后无法正常获取到目录下面的所有需要查找的文件,只会返回第一个匹配的文件。想了半天 不知道代码的逻辑哪错了
修改如下,其中os.isdir判断的是绝对路径,你的事相对路径。
- import os
- def Find_Name(target, FilePath):
- print("当前目录:",FilePath)
- for name in os.listdir(FilePath):
- print("当前目录:%s,是否目录:%s"%(name,os.path.isdir(os.path.join(FilePath, name))))
- if os.path.isdir(os.path.join(FilePath, name)):
- print(name)
- Find_Name(target, os.path.join(FilePath, name))
- else:
- if name == target:
- print("符合条件的文件%s"%os.path.join(FilePath, name))
-
- FilePath = r"C:\Users\Administrator\Desktop\123"
- FindName ="2020.txt"
- Find_Name(FindName, FilePath)
复制代码
|
|