代码如下:
特定类型
# -*- coding: utf-8 -*-
import os
path = os.getcwd() #获取当前文件夹位置
print(path)
readPath = []
for parent, dirnames, filenames in os.walk(path):
for filename in filenames:
if '.xls' in filename :
file_path = os.path.join(parent, filename)
readPath.append(file_path)
print(file_path)
print(readPath)
或多种类型
# -*- coding: utf-8 -*-
import os
path = os.getcwd() #获取当前文件夹位置
print(path)
readPath = []
types = ['xlsx','csv']
for parent, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename.split('.')[-1] in types :
file_path = os.path.join(parent, filename)
readPath.append(file_path)
print(file_path)
print(readPath)
或带有特点名称字符的文件名
# -*- coding: utf-8 -*-
import os
path = os.getcwd() #获取当前文件夹位置
print(path)
readPath = []
for parent, dirnames, filenames in os.walk(path):
for filename in filenames:
if 'findCharacters' in filename :
file_path = os.path.join(parent, filename)
readPath.append(file_path)
print(file_path)
print(readPath)