#T2 dcm文件从原位置批量复制到新文件夹'F:/467289/'
import os
import numpy as np
path = '/Users/yxk/Desktop/Debug_test/file'# for root, dirs, files in os.walk(path):
# for file in files:
# if file.endswith('.dcm'):
# print(file)# def findPath(startDir,targetFile):
# file_count = 0
# for i,(current_dir,dirs,files) in enumerate(os.walk(startDir)):
# file_count += len(files)
# if targetFile in files:
# return (i,file_count,os.path.join(current_dir,targetFile))
# return (i,file_count,None)# print(findPath(path,"000018.dcm"))def walkfs(startdir, findfile):dircount = 0filecount = 0file_list=[]parent_list=[]for root, dirs, files in os.walk(startdir):#print(files)if findfile in files:#return dircount, filecount + files.index(findfile), os.path.join(root, findfile)file_list.append(os.path.join(root, findfile))parent_dir = os.path.dirname(os.path.join(root, findfile))_, last_parent_folder = os.path.split(parent_dir)print(last_parent_folder)parent_list.append(parent_dir)# dircount += 1# filecount += len(files)# nothing found, return None instead of a full path for the filereturn file_list,parent_list
a,b = walkfs(path,"000018.dcm")
print(a)
print("*"*50)
print(b)# import os
# directory =path
# extension = "dcm"# pdf_files = []
# for root, dirs, files in os.walk(directory):
# for filename in files:
# if filename.endswith("." + extension):
# #print(filename)
# pdf_files.append(filename)# print(pdf_files) # print all collected files
# print(len(pdf_files))
# #print(pdf_files[2]) # print third collected file
加入一个判断条件
def walkfs(startdir, findfile):dircount = 0filecount = 0file_list=[]parent_list=[]for root, dirs, files in os.walk(startdir):#print(files)if findfile in files:#return dircount, filecount + files.index(findfile), os.path.join(root, findfile)parent_dir = os.path.dirname(os.path.join(root, findfile))_, last_parent_folder = os.path.split(parent_dir)if ('t1' in last_parent_folder) or ('T1' in last_parent_folder):file_list.append(os.path.join(root, findfile))# print(last_parent_folder)#parent_list.append(parent_dir)return file_list
a = walkfs(path,"000018.dcm")
print(a)
print("*"*50)
以一个后缀名寻找
def walkfs(startdir, extension="dcm"):dircount = 0filecount = 0file_list=[]parent_list=[]for root, dirs, files in os.walk(startdir):#print(files)for findfile in files:#return dircount, filecount + files.index(findfile), os.path.join(root, findfile)if findfile.endswith("." + extension):parent_dir = os.path.dirname(os.path.join(root, findfile))_, last_parent_folder = os.path.split(parent_dir)if ('t1' in last_parent_folder) or ('T1' in last_parent_folder):file_list.append(os.path.join(root, findfile))# print(last_parent_folder)#parent_list.append(parent_dir)return file_list
a = walkfs(path,"dcm")
print(a)
print(len(a))
print("*"*50)
在这里插入代码片