獲取文件夾下某一個格式的所有文件的文件名,主要是工作需要,手動獲取文件名太麻煩了,整理一下:
#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列表