def find_file(str,path="."):
file_pwd=[]
def fun(str,path="."):
now_path=os.path.abspath(path)
file_list=[x for x in os.listdir(now_path) if os.path.isfile(os.path.join(now_path,x))]
dir_list=[x for x in os.listdir(now_path) if os.path.isdir(os.path.join(now_path,x))]
for i in file_list:
if str in i:
file=[]
file.append(os.path.getsize(os.path.join(now_path,i)))
file.append(datetime.fromtimestamp(os.path.getmtime(os.path.join(now_path,i))).strftime("%Y-%m-%d %H:%M"))
file.append(i)
file.append(os.path.abspath(i))
file_pwd.append(file)
for i in dir_list:
new_path=os.path.join(now_path,i)
fun(str,new_path)
fun(str,path)
for i in file_pwd:
print(i)
return file_pwd
花朝zhe
import os from datetime import datetime
def find_file(str,path="."): file_pwd=[] def fun(str,path="."): now_path=os.path.abspath(path) file_list=[x for x in os.listdir(now_path) if os.path.isfile(os.path.join(now_path,x))] dir_list=[x for x in os.listdir(now_path) if os.path.isdir(os.path.join(now_path,x))] for i in file_list: if str in i: file=[] file.append(os.path.getsize(os.path.join(now_path,i))) file.append(datetime.fromtimestamp(os.path.getmtime(os.path.join(now_path,i))).strftime("%Y-%m-%d %H:%M")) file.append(i) file.append(os.path.abspath(i)) file_pwd.append(file) for i in dir_list: new_path=os.path.join(now_path,i) fun(str,new_path) fun(str,path) for i in file_pwd: print(i) return file_pwd
x=find_file("test")