p
y
t
h
o
n
读
取
文
件
下
所
有
文
件
路
径
python读取文件下所有文件路径
python读取文件下所有文件路径
一
def read_file(path): # 图片的完整路径
import os
"""从文件夹中读取数据"""
files_list = os.listdir(path)
file_path_list = [os.path.join(path, img) for img in files_list]
file_path_list.sort() # 图片路径排序
return file_path_list
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
二
def get_file_path_by_name(file_dir):
import os
'''
获取指定路径下所有文件的绝对路径
:param file_dir:
:return:
'''
L = []
for root, dirs, files in os.walk(file_dir): # 获取所有文件
for file in files: # 遍历所有文件名
if os.path.splitext(file)[1] == '.png': # 指定尾缀 ***重要***
L.append(os.path.join(root, file)) # 拼接处绝对路径并放入列表
print('总文件数目:', len(L))
return L
list_dir = get_file_path_by_name(r'C:\Users\29939\Desktop\dataset')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17