如何从目录列表中读取文件?

时间:2018-09-21 02:55:51

标签: python

在这里,我有很多病人折纸,每折有10张图像。 我有一个列表,其中存储了我想要的一些目录。

如何仅从列表(数据路径)上的目录加载/读取文件?

# data_paths is the list
data_paths = ['./data/preprocessed_data/train/Patient009969', './data/preprocessed_data/train/Patient009928', './data/preprocessed_data/train/Patient009966'] 
data_and_seg_arr = [np.load(ii, mmap_mode='r') for ii in data_paths]


Eror:
IsADirectoryError: [Errno 21] Is a directory: './data/preprocessed_data/train/Patient009969'

我需要保留此数据表格。因为我必须要做:

pids = [ii.split('/')[-1].split('.')[0] for ii in data_paths]
data = OrderedDict()
for ix, pid in enumerate(pids):
    data[pid] = {'data': data_and_seg_arr[ix][..., 0], 'seg': data_and_seg_arr[ix][..., 1], 'pid': pid}
return data

3 个答案:

答案 0 :(得分:0)

您是否曾经尝试仅从列表中加载一个文件?
也许您应该检查“文件的属性”,因为我认为异常Errno 21是由Python open()而不是numpy引发的。
parse_qs

整个错误消息会有所帮助。

答案 1 :(得分:0)

您将需要创建另一个循环,循环遍历路径列表中的文件。

(playerName: "Bam Bam", completionTime: (minutes: 1, seconds: 10, milliseconds: 0), totalMoves: 3, dateStamp: 2018-09-21 11:17:36 +0000)
(playerName: "Pebbles", completionTime: (minutes: 4, seconds: 10, milliseconds: 799), totalMoves: 3, dateStamp: 2018-09-21 11:17:36 +0000)
(playerName: "Fred", completionTime: (minutes: 4, seconds: 10, milliseconds: 800), totalMoves: 3, dateStamp: 2018-09-21 11:17:36 +0000)
(playerName: "Wilma", completionTime: (minutes: 4, seconds: 10, milliseconds: 801), totalMoves: 3, dateStamp: 2018-09-21 11:17:36 +0000)
(playerName: "Barney", completionTime: (minutes: 5, seconds: 10, milliseconds: 800), totalMoves: 3, dateStamp: 2018-09-21 11:17:36 +0000)

答案 2 :(得分:0)

假设所有文件均为jpg或png格式

path="/path/to/image"
#NOTE THIS IS REALLY IMPORTANT
#IF YOUR PATH IS LIKE THIS /path/to/image YOU NEED TO ADD A / AT THE END, IF 
#YOUR PATH ALREADY HAVE THE "/" AT THE END YOU DON'T NEED TO ADD IF
#EXAMPLE PROBLEM PATH:/path/to/image
#PATH WITH NO PROBLEM /path/to/image/
jpg = glob.glob(path + "/*.jpg")
png = glob.glob(path + "/*.png")
imagelist=jpg+png


>>>print(type(imagelist))
<class 'list'>

希望这可以解决您的问题