gpt4 book ai didi

python - 如何在Python中获取目录下的文件类型

转载 作者:行者123 更新时间:2023-12-01 05:22:17 26 4
gpt4 key购买 nike

我有这样的目录结构

Root\
DirA\
DirX\
File.aaa
File.bbb
File.ccc
File.ddd
DirB\
File.aaa
File.ccc
File.ddd
File.eee
File.fff

使用 python 我想获取所有文件类型的列表,如下所示:

['aaa','bbb','ccc','ddd','eee','fff']

最佳答案

m. samy's answer是正确的。然而,使用一个可以神奇地检查重复项的集合会更有效。

import os

def get_file_types(directory):
file_ext = set()
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
fileName, fileExtension = os.path.splitext(filepath)


return sorted(file_ext) # Self-explanatory.


file_types = get_file_types("d:\\Development\\")
for ty in file_types:
print ty

关于python - 如何在Python中获取目录下的文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22084442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com