我已经编写了以下Python代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, glob
path = '/home/my/path'
for infile in glob.glob( os.path.join(path, '*.png') ):
print infile
现在我得到了这个:
/home/my/path/output0352.png
/home/my/path/output0005.png
/home/my/path/output0137.png
/home/my/path/output0202.png
/home/my/path/output0023.png
/home/my/path/output0048.png
/home/my/path/output0069.png
/home/my/path/output0246.png
/home/my/path/output0071.png
/home/my/path/output0402.png
/home/my/path/output0230.png
/home/my/path/output0182.png
/home/my/path/output0121.png
/home/my/path/output0104.png
/home/my/path/output0219.png
/home/my/path/output0226.png
/home/my/path/output0215.png
/home/my/path/output0266.png
/home/my/path/output0347.png
/home/my/path/output0295.png
/home/my/path/output0131.png
/home/my/path/output0208.png
/home/my/path/output0194.png
它是如何排列的?
澄清一下:我对排序不感兴趣——我知道排序。我想知道它默认出现的顺序。
它可能会帮助你得到我的ls -l输出:
-rw-r--r-- 1 moose moose 627669 2011-07-17 17:26 output0005.png
-rw-r--r-- 1 moose moose 596417 2011-07-17 17:26 output0023.png
-rw-r--r-- 1 moose moose 543639 2011-07-17 17:26 output0048.png
-rw-r--r-- 1 moose moose 535384 2011-07-17 17:27 output0069.png
-rw-r--r-- 1 moose moose 543216 2011-07-17 17:27 output0071.png
-rw-r--r-- 1 moose moose 561776 2011-07-17 17:27 output0104.png
-rw-r--r-- 1 moose moose 501865 2011-07-17 17:27 output0121.png
-rw-r--r-- 1 moose moose 547144 2011-07-17 17:27 output0131.png
-rw-r--r-- 1 moose moose 530596 2011-07-17 17:27 output0137.png
-rw-r--r-- 1 moose moose 532567 2011-07-17 17:27 output0182.png
-rw-r--r-- 1 moose moose 553562 2011-07-17 17:27 output0194.png
-rw-r--r-- 1 moose moose 574065 2011-07-17 17:27 output0202.png
-rw-r--r-- 1 moose moose 552197 2011-07-17 17:27 output0208.png
-rw-r--r-- 1 moose moose 559809 2011-07-17 17:27 output0215.png
-rw-r--r-- 1 moose moose 549046 2011-07-17 17:27 output0219.png
-rw-r--r-- 1 moose moose 566661 2011-07-17 17:27 output0226.png
-rw-r--r-- 1 moose moose 561678 2011-07-17 17:27 output0246.png
-rw-r--r-- 1 moose moose 525550 2011-07-17 17:27 output0266.png
-rw-r--r-- 1 moose moose 565715 2011-07-17 17:27 output0295.png
-rw-r--r-- 1 moose moose 568381 2011-07-17 17:28 output0347.png
-rw-r--r-- 1 moose moose 532768 2011-07-17 17:28 output0352.png
-rw-r--r-- 1 moose moose 535818 2011-07-17 17:28 output0402.png
它不是按文件名或大小排序的。
其他链接:glob, ls
'''my file name is
"0_male_0.wav", "0_male_2.wav"... "0_male_30.wav"...
"1_male_0.wav", "1_male_2.wav"... "1_male_30.wav"...
"8_male_0.wav", "8_male_2.wav"... "8_male_30.wav"
when I wav.read(files) I want to read them in a sorted torder, i.e., "0_male_0.wav"
"0_male_1.wav"
"0_male_2.wav" ...
"0_male_30.wav"
"1_male_0.wav"
"1_male_1.wav"
"1_male_2.wav" ...
"1_male_30.wav"
so this is how I did it.
Just take all files start with "0_*" as an example. Others you can just put it in a loop
'''
import scipy.io.wavfile as wav
import glob
from os.path import isfile, join
#get all the file names in file_names. THe order is totally messed up
file_names = [f for f in listdir(audio_folder_dir) if isfile(join(audio_folder_dir, f)) and '.wav' in f]
#find files that belongs to "0_*" group
filegroup0 = glob.glob(audio_folder_dir+'/0_*')
#now you get sorted files in group '0_*' by the last number in the filename
filegroup0 = sorted(filegroup0, key=getKey)
def getKey(filename):
file_text_name = os.path.splitext(os.path.basename(filename)) #you get the file's text name without extension
file_last_num = os.path.basename(file_text_name[0]).split('_') #you get three elements, the last one is the number. You want to sort it by this number
return int(file_last_num[2])
这就是我处理特定情况的方法。希望对大家有帮助。
我有一个类似的问题,glob返回一个任意顺序的文件名列表,但我想通过它们的数字顺序,如文件名所示。我是这样做到的:
我的文件被glob返回,类似于:
myList = ["c:\tmp\x\123.csv", "c:\tmp\x\44.csv", "c:\tmp\x\101.csv", "c:\tmp\x\102.csv", "c:\tmp\x\12.csv"]
我对列表进行排序,为此我创建了一个函数:
def sortKeyFunc(s):
return int(os.path.basename(s)[:-4])
此函数返回文件名的数字部分并转换为整数。然后我在列表上调用sort方法:
myList.sort(key=sortKeyFunc)
返回一个列表,如下所示:
["c:\tmp\x\12.csv", "c:\tmp\x\44.csv", "c:\tmp\x\101.csv", "c:\tmp\x\102.csv", "c:\tmp\x\123.csv"]
'''my file name is
"0_male_0.wav", "0_male_2.wav"... "0_male_30.wav"...
"1_male_0.wav", "1_male_2.wav"... "1_male_30.wav"...
"8_male_0.wav", "8_male_2.wav"... "8_male_30.wav"
when I wav.read(files) I want to read them in a sorted torder, i.e., "0_male_0.wav"
"0_male_1.wav"
"0_male_2.wav" ...
"0_male_30.wav"
"1_male_0.wav"
"1_male_1.wav"
"1_male_2.wav" ...
"1_male_30.wav"
so this is how I did it.
Just take all files start with "0_*" as an example. Others you can just put it in a loop
'''
import scipy.io.wavfile as wav
import glob
from os.path import isfile, join
#get all the file names in file_names. THe order is totally messed up
file_names = [f for f in listdir(audio_folder_dir) if isfile(join(audio_folder_dir, f)) and '.wav' in f]
#find files that belongs to "0_*" group
filegroup0 = glob.glob(audio_folder_dir+'/0_*')
#now you get sorted files in group '0_*' by the last number in the filename
filegroup0 = sorted(filegroup0, key=getKey)
def getKey(filename):
file_text_name = os.path.splitext(os.path.basename(filename)) #you get the file's text name without extension
file_last_num = os.path.basename(file_text_name[0]).split('_') #you get three elements, the last one is the number. You want to sort it by this number
return int(file_last_num[2])
这就是我处理特定情况的方法。希望对大家有帮助。