我是初学者。我想读取文件夹中的所有文件。例如,文件名为1.csv,2.csv ........ 10.csv,11.csv .. ... 20.csv同样。它sholud读作1.csv,2.csv ......但对我来说它是1.csv,10.csv,11.csv ....... 19.csv,2.csv
我正在使用代码:
import glob
path = 'C://test//08October2014//DATA_INTV_NEW//October082014//*.sec.gz'
files=glob.glob(path)
for list in sorted(files):
print list
输出:
C://test//08October2014//DATA_INTV_NEW//October082014\1.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\10.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\11.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\12.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\13.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\14.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\15.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\16.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\17.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\18.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\19.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\2.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\20.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\21.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\22.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\23.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\24.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\25.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\26.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\27.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\28.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\29.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\3.sec.gz
C://test//08October2014//DATA_INTV_NEW//October082014\30.sec.gz
答案 0 :(得分:1)
在字符串比较中,"0" < "1" < "10" < "2" < "20"
(词典顺序)。您必须为文件命名"01"
,"02"
,...,"09"
,"10"
,...以便他们无需额外努力即可正确排序。
如果你不能这样做,那么你所寻找的东西被称为&#34;自然分类&#34;。这里有一个模块:https://pypi.python.org/pypi/natsort