【问题标题】:Get names in a directory in order in python在python中按顺序获取目录中的名称
【发布时间】:2018-08-09 16:05:32
【问题描述】:

我有一个目录,其中的图片名称如下: v1-001.png v1-002.png ...... 我想按顺序列出名称(V1--001 V1-002 V1--003 V1--004),但我有这个 v1--00900 v1--002 v1--0034 V1--0020 V1--001

这是我的代码:

import os
path="."

 for current_dir, dir_names, file_names in os.walk(path):
    for file_name in file_names:
        in_file = os.path.join(current_dir, file_name)
        print(in_file)

【问题讨论】:

  • 代码给你什么?

标签: python python-3.x python-2.7


【解决方案1】:

您可以简单地使用sorted() 对文件名列表进行排序:

for current_dir, dir_names, file_names in os.walk(path):
    for file_name in sorted(file_names):
        in_file = os.path.join(current_dir, file_name)
        print(in_file)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多