Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中按顺序获取目录中的名称_Python_Python 3.x_Python 2.7 - Fatal编程技术网

在python中按顺序获取目录中的名称

在python中按顺序获取目录中的名称,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我有一个包含图片的目录,这些图片的名称如下: 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

我有一个包含图片的目录,这些图片的名称如下: 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)

您只需使用
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)