python获取文件夹下某一格式的所有文件名称

获取文件夹下某一个格式的所有文件的文件名,主要是工作需要,手动获取文件名太麻烦了,整理一下:

#encoding=utf-8

import os

#os.walk方法获取当前路径下的root(所有路径)、dirs(所有子文件夹)、files(所有文件)

def file_name(self):
F = []
for root, dirs, files in os.walk(os.path.dirname(os.path.dirname(__file__))):
#print root
#print dirs
for file in files:
#print file.decode('gbk') #文件名中有中文字符时转码
if os.path.splitext(file)[1] == '.py':
t = os.path.splitext(file)[0]
print t #打印所有py格式的文件名
L.append(t) #将所有的文件名添加到L列表中
return F 返回L列表
posted @ 2017-12-18 11:03  欣怡巴巴  阅读(4656)  评论(0编辑  收藏  举报