【发布时间】:2021-09-01 01:31:04
【问题描述】:
我正在从一个目录中获取所有文件名列表
但是当我排序时,它不是根据final.txt 文件中的数字文件名排序的
我的 Python 代码:
pth=r'/d/demo'
fname=glob.glob(pth+"/['ABC']*.txt")
nlist='/d/bk/final.txt'
fname.sort()
for i in fname:
file=os.path.basename(i)
nlist.write(file+os.linesep)
以上代码输出格式如下 final.txt 文件
ABC1.txt
ABC11.txt
ABC10.txt
ABC2.txt
ABC3.txt
ABC4.txt
ABC5.txt
ABC6.txt
ABC7.txt
ABC8.txt
ABC9.txt
ABC13.txt
ABC12.txt
以下格式的预期输出:
ABC1.txt
ABC2.txt
ABC3.txt
ABC4.txt
ABC5.txt
ABC6.txt
ABC7.txt
ABC8.txt
ABC9.txt
ABC10.txt
ABC11.txt
ABC12.txt
ABC13.txt
【问题讨论】:
标签: python