python获取指定目录下所有文件名列表的方法
作者:网海水手 时间:2022-03-24 08:05:39
本文实例讲述了python获取指定目录下所有文件名列表的方法。分享给大家供大家参考。具体实现方法如下:
这里python代码实现获取文件名列表的功能,可以指定文件中包含的字符,方便提取特定类型的文件名列表:
# -*- coding: utf-8 -*-
#~ #------------------------------------------------------------------
#~ module:wlab
#~ Filename:wgetfilelist.py
#~ Function :
#~ def IsSubString(SubStrList,Str)
#~ def GetFileList(FindPath,FlagStr=[]):
#~ 功能:读取指定目录下特定类型的文件名列表
#~ Data: 2013-08-08,星期四
#~ Author:吴徐平
#~ Email:wxp07@qq.com
#~ #------------------------------------------------------------------
#~ #------------------------------------------------------------------
def IsSubString(SubStrList,Str):
'''''
#判断字符串Str是否包含序列SubStrList中的每一个子字符串
#>>>SubStrList=['F','EMS','txt']
#>>>Str='F06925EMS91.txt'
#>>>IsSubString(SubStrList,Str)#return True (or False)
'''
flag=True
for substr in SubStrList:
if not(substr in Str):
flag=False
return flag
#~ #----------------------------------------------------------------------
def GetFileList(FindPath,FlagStr=[]):
'''''
#获取目录中指定的文件名
#>>>FlagStr=['F','EMS','txt'] #要求文件名称中包含这些字符
#>>>FileList=GetFileList(FindPath,FlagStr) #
'''
import os
FileList=[]
FileNames=os.listdir(FindPath)
if (len(FileNames)>0):
for fn in FileNames:
if (len(FlagStr)>0):
#返回指定类型的文件名
if (IsSubString(FlagStr,fn)):
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
else:
#默认直接返回所有文件名
fullfilename=os.path.join(FindPath,fn)
FileList.append(fullfilename)
#对文件名排序
if (len(FileList)>0):
FileList.sort()
return FileList
可以使用pip在线安装wlab
pip install wlab
还是给个图吧:
希望本文所述对大家的Python程序设计有所帮助。
标签:python,目录,文件名
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
django 中的聚合函数,分组函数,F 查询,Q查询
2021-07-24 02:37:50
DataReader深入解析:持续更新
2023-07-11 06:24:08
在Python中处理日期和时间的基本知识点整理汇总
2021-05-13 07:12:14
![](https://img.aspxhome.com/file/2023/9/89079_0s.jpg)
PHP数组中头部和尾部添加元素的方法(array_unshift,array_push)
2023-06-06 22:45:50
![](https://img.aspxhome.com/file/2023/2/55472_0s.png)
Ubuntu16安装Python3.9的实现步骤
2021-01-23 14:04:53
![](https://img.aspxhome.com/file/2023/7/92397_0s.jpg)
python实现的config文件读写功能示例
2021-10-11 07:28:04
asp.net DropDownList实现二级联动效果
2023-07-23 07:48:41
![](https://img.aspxhome.com/file/2023/6/79976_0s.jpg)
数据库主键的故事
2008-05-31 07:50:00
python 获取微信好友列表的方法(微信web)
2022-02-20 10:05:21
Python下调用Linux的Shell命令的方法
2023-08-10 01:43:17
ASP使用wsImage组件给图片加水印
2008-12-29 19:14:00
Python字符编码转码之GBK,UTF8互转
2023-02-20 14:03:01
![](https://img.aspxhome.com/file/2023/8/102998_0s.png)
基于python3 的百度图片下载器的实现代码
2021-07-15 00:22:34
Python translator使用实例
2021-02-09 22:40:24
使用pyqt5 实现ComboBox的鼠标点击触发事件
2022-01-12 17:24:57
python numpy实现多次循环读取文件 等间隔过滤数据示例
2022-10-30 09:44:13
![](https://img.aspxhome.com/file/2023/8/65458_0s.jpg)
帮助你分析MySQL的数据类型以及建库策略
2009-02-23 17:39:00
Python面向对象基础入门之设置对象属性
2021-10-21 19:25:10
pytorch实现focal loss的两种方式小结
2023-07-02 14:43:22
详解Django+Uwsgi+Nginx的生产环境部署
2022-10-30 10:54:17
![](https://img.aspxhome.com/file/2023/3/89193_0s.png)