python OS.walk以随机顺序打印我的文件,我如何对值进行排序?

umuewwlo  于 2023-04-28  发布在  Python
关注(0)|答案(1)|浏览(156)

我试图从一个文件夹中导出六个图像,这六个图像被称为1。png,2.png等,但当我调用os.走着走着,要求打印出的文件,他们随机排列:

/Users/claudiabergeron/Documents/Python/Python_VideoGame_3.10/bin/python "/Users/claudiabergeron/Documents/Python/Tutoriels/Video games/Pygame/Platformer/help.py"
('/Users/claudiabergeron/Documents/Python/Tutoriels/Video games/Pygame/Platformer/graphics/character/run', [], ['4.png', '5.png', '6.png', '2.png', '3.png', '1.png'])

代码如下:

from os import walk
def import_folder(path):

    for information in walk(path):
        print(information)

import_folder('/Users/leolepage/Documents/Python/Tutoriels/Video games/Pygame/Platformer/graphics/character/run')

有人知道我该怎么做吗?

j5fpnvbx

j5fpnvbx1#

os.walk使用os。listdir和不能排序。我建议你用操作系统。listdir并排序:

from os import listdir
def import_folder(path):
    for information in sorted(listdir(path)):
        print(information)

相关问题