[Python] How to sort files inside a directory to follow order by file size?

Discussão em 'Python' iniciado por Stack, Junho 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have a directory in which I have 3 files, 2 being directories and another which is a mimetype. I want to keep the mimetype as the first file in the directory, so I made the mimetype first inside the directory. But when I transform that directory into an archive with shutil.make_archive() the resulting archive has the mimetype at the bottom of the directory. Any way to sort the files before using shutil.make_archive() so that the mimetype doesn't end up at the bottom of the directory?

    Code currently looks like this:

    os.makedires("mainDirectory")
    os.chdir("mainDirectory")

    file = open("mimetype", "w", encoding="ASCII")
    file.write("...")

    os.makedirs("directory1")
    os.makedirs("directory2")
    os.chdir("directory1")
    ... #changes to directory 1

    os.chdir("directory2")
    ... #changes to directory 2

    os.chdir("mainDirectory")
    make_archive("mainDirectory", "zip", "mainDirectory")

    Continue reading...

Compartilhe esta Página