python找到文件夹下指定文件_python笔记之按文件名搜索指定路径下的文件

1.搜索文件名中以指定的字符串开头(如搜索dll,结果中含有dll a,dll abc等)

我的目录下有dll a.txt和dll.txt文件

1373746-20180506124517478-1651946128.png

其中a文件夹下还有这两个文件

1373746-20180506124534642-1662792669.png

我希望通过python选择寻找关键字dll来把这四个文件找出

import os

result=[]

def search(path=".", name=""):

for item in os.listdir(path):

item_path = os.path.join(path, item)

if os.path.isdir(item_path):

search(item_path, name)

elif os.path.isfile(item_path):

if name in item:

global result

result.append(item_path + ";")

print (item_path + ";", end="")

search(path=r"D:\newfile", name="dll")

输出结果:

1373746-20180506124634172-405783374.png

2.如果我只想找出名叫dll的txt,不要dll a.txt,即上文是关键字匹配,这次改为全匹配。那么就要用到搜索指定的文件名

只要将上文代码中

if name in item:

改为

if name+".txt" == item:

即可

3.提取excel某一列中病人姓名并转化为拼音,根据拼音检索某一路径下的对应图片路径

#-*-coding:utf-8-*-

importxlrdimportosimportrefrom xlwt import *

from xpinyin importPinyin

pa=None

myItem_path=[]deffile_name(file_dir):for root, dirs, files inos.walk(file_dir):return(dirs) #当前路径下所有非目录子文件

def search(path=".", name="1"):for item inos.listdir(path):globalpa

item_path=os.path.join(path, item)ifos.path.isdir(item_path):

search(item_path, name)#if(t==None):pa=None

elifos.path.isfile(item_path):if name+".jpg" ==item:

myItem_path.append(item_path+";")print (item_path+";",end="")

pa=myItem_path#------------------读数据--------------------------------

fileName="D:\\study\\xmu\\420\\廖希一\\数字化之后\\上机名单-2014,2015.xls"bk=xlrd.open_workbook(fileName)

shxrange=range(bk.nsheets)try:

sh=bk.sheet_by_name("2014年")except:print ("代码出错")

nrows=sh.nrows #获取行数

book = Workbook(encoding='utf-8')

sheet= book.add_sheet('Sheet1') #创建一个sheet

p =Pinyin()for i in range(1,nrows):#row_data=sh.row_values(i)

#获取第i行第3列数据

# #---------写出文件到excel--------

#if i==16:

#break

a=p.get_pinyin( sh.cell_value(i,2), ' ')

search(path=r"D:\study\xmu\420\廖希一\photo", name=a)

myItem_path=[]print ("-----正在写入"+str(i)+"行")

sheet.write(i,0, label= sh.cell_value(i,2)) #向第1行第1列写入获取到的值

sheet.write(i,1, label = sh.cell_value(i,4))#向第1行第2列写入获取到的值

sheet.write(i,2, label=a)

sheet.write(i,3, label=pa)

pa=None

book.save("D:\\study\\xmu\\420\\廖希一\\数字化之后\\上机名单-2014+图片路径.xls")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python的os和shutil模块来实现这个任务。下面是一个示例代码: ```python import os import shutil def get_files_with_specific_name(directory, file_name, output_directory): # 获取目录下的所有文件夹 subdirectories = [name for name in os.listdir(directory) if os.path.isdir(os.path.join(directory, name))] for subdirectory in subdirectories: # 构建当前子文件夹的完整路径 subdirectory_path = os.path.join(directory, subdirectory) # 获取当前子文件夹下的所有文件 files = [name for name in os.listdir(subdirectory_path) if os.path.isfile(os.path.join(subdirectory_path, name))] # 寻指定文件名文件并复制到输出目录中 for file in files: if file == file_name: output_subdirectory = os.path.join(output_directory, subdirectory) os.makedirs(output_subdirectory, exist_ok=True) # 创建输出子文件夹 source_file = os.path.join(subdirectory_path, file) destination_file = os.path.join(output_subdirectory, file) shutil.copy(source_file, destination_file) print(f"已复制文件: {file} 到目录: {output_subdirectory}") # 指定目录和输出目录 directory = 'path/to/directory' output_directory = 'path/to/output_directory' file_name = 'example.txt' # 指定文件名 # 调用函数获取指定文件名文件并保存到指定文件夹 get_files_with_specific_name(directory, file_name, output_directory) ``` 在上面的示例代码中,你需要将`directory`替换为你想要搜索的目录路径,将`output_directory`替换为你想要保存指定文件名文件的目录路径,将`file_name`替换为你想要获取的特定文件名。代码会遍历指定目录下的所有文件夹,并将符合指定文件名文件复制到指定的输出目录中的子文件夹中。 请确保在运行代码之前已经安装了必要的模块(os、shutil),并将目录路径文件名替换为你自己的实际路径文件名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值