Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在迭代一堆文件,如下所示:
for file in glob('./*.dat'): print file
输出始终如下:
./SAN0.dat ./SAN4.dat ./SAN1.dat ./SAN2.dat ./SAN3.dat ./SAN5.dat ./SAN6.dat ./SAN7.dat
我如何按名称顺序迭代它们(SAN1.dat例如,意思是第二个)?
SAN1.dat
谢谢!
for file in sorted(glob('./*.dat')):
lst = glob('./*.dat') lst.sort()
以下是在python中按文件名顺序迭代文件的最简单方法-
import os for file in sorted(os.listdir(path))