我试图在给定扩展名的目录中对文件进行排序,但提供了我首先给出的顺序。假设我希望延期订单是
ext_list = [ .bb, .cc , .dd , aa ]
我能想到的唯一方法是在每次遇到特定扩展名时遍历每个文件并将它们放在一个列表中。
for subdir, dirs, files in os.walk(directory):
if file.endswith( '.bb') --> append file
then go to the end of the directory
then loop again
if file.endswith( '.cc') -->append file
and so on...
return sorted_extension_list
然后最后
for file in sorted_extension_list :
print file