Pythons glob.glob는 어떻게 주문됩니까?
다음 파이썬 코드를 작성했습니다.
#!/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
파일 이름 또는 크기별로 정렬되지 않습니다.
아마도 전혀 정렬되지 않았으며 파일 시스템에 항목이 나타나는 순서, 즉 사용할 때 얻는 순서를 사용 ls -U
합니다. (적어도 내 컴퓨터에서는 리스팅 glob
일치와 동일한 순서를 생성합니다 ).
순서는 임의적이지만 직접 정렬 할 수 있습니다
이름별로 정렬하려면 :
sorted(glob.glob('*.png'))
수정 시간별로 정렬 :
import os
sorted(glob.glob('*.png'), key=os.path.getmtime)
크기별로 정렬 :
import os
sorted(glob.glob('*.png'), key=os.path.getsize)
기타
소스 코드를 확인하면 다음 과 같이 glob.glob
내부적으로 호출 os.listdir
됩니다.
http://docs.python.org/library/os.html?highlight=os.listdir#os.listdir
Key sentence: os.listdir(path) Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Arbitrary order. :)
glob.glob() is a wrapper around os.listdir() so the underlaying OS is in charge for delivering the data. In general: you can not make an assumption on the ordering here. The basic assumption is: no ordering. If you need some sorting: sort on the application level.
I had a similar issue, glob
was returning a list of file names in an arbitrary order but I wanted to step through them in numerical order as indicated by the file name. This is how I achieved it:
My files were returned by glob
something like:
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"]
I sorted the list in place, to do this I created a function:
def sortKeyFunc(s):
return int(os.path.basename(s)[:-4])
This function returns the numeric part of the file name and converts to an integer.I then called the sort method on the list as such:
myList.sort(key=sortKeyFunc)
This returned a list as such:
["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"]
Order is arbitrary, but there are several ways to sort them. One of them is as following:
#First, get the files:
import glob
import re
files =glob.glob1(img_folder,'*'+output_image_format)
# if you want sort files according to the digits included in the filename, you can do as following:
files = sorted(files, key=lambda x:float(re.findall("(\d+)",x)[0]))
If you're wondering about what glob.glob has done on your system in the past and cannot add a sorted
call, the ordering will be consistent on Mac HFS+ filesystems and will be traversal order on other Unix systems. So it will likely have been deterministic unless the underlying filesystem was reorganized which can happen if files were added, removed, renamed, deleted, moved, etc...
Please try this code:
sorted(glob.glob( os.path.join(path, '*.png') ),key=lambda x:float(re.findall("([0-9]+?)\.png",x)[0]))
'''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])
That's how I did my particular case. Hope it's helpful.
참고URL : https://stackoverflow.com/questions/6773584/how-is-pythons-glob-glob-ordered
'IT' 카테고리의 다른 글
iOS 7에서 UIButton 이미지를 설정하면 파란색 버튼이 나타납니다. (0) | 2020.06.03 |
---|---|
log4j 로깅 계층 순서 (0) | 2020.06.03 |
자식 행을 추가하거나 업데이트 할 수 없습니다 : 외래 키 제약 조건이 실패합니다 (0) | 2020.06.03 |
jruby에서 float를 소수점 이하 두 자리로 반올림하는 방법 (0) | 2020.06.03 |
명령 줄 도구-오류-xcrun : 오류 : 개발자 도구가 아닌 PATH에서“xcodebuild”유틸리티를 찾을 수 없습니다. (0) | 2020.06.03 |