我想读取目录中所有'mp4'文件的名称,我需要将每个文件的名称写在csv文件的行中。但问题是所有名称都写在不同的列中。我如何解决这个问题?
import csv
import os
Names = []
path_and_label =[]
for file in os.listdir("/mnt/storage/user/kinetics400/train_256/abseiling"):
#path_and_label =[]
if file.endswith(".mp4"):
full_address = os.path.normpath(os.path.join("/mnt/storage_6TB/user/kinetics400/train_256/abseiling", file))
path_components = full_address.split(os.sep)
Label = path_components[6]
path_and_label.append(full_address + ' ' + Label)
with open('/mnt/storage/user/kinetics400/my_kinetics.csv', 'w+', newline ='') as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerow(path_and_label)
f.close()
1条答案
按热度按时间dfuffjeb1#
你非常接近了。试试下面的代码。我不知道你想用Label=path_component[6]打印什么,我已经注解掉了那部分。
上面的代码将下面的结果写入csvfile
递归搜索路径的代码。