Как заставить os.listdir перечислять файлы по порядку [дубликат]Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как заставить os.listdir перечислять файлы по порядку [дубликат]

Сообщение Anonymous »


I'm wanting to rename all of the files in a directory. When sorted by name in Windows Explorer, they appear in the correct order:

Cue 001 - 002.mp3 ... Cue 001 - 010.mp3 Cue 001 - 011.mp3 ... Cue 001 - 020.mp3 Cue 001 - 021.mp3 ... Cue 001 - 0111.mp3 Cue 001 - 0112.mp3 So far I've been able to use os.chdir to change the working directory to the directory having the target files, and confirm by using print (os.getcwd()).

import os # Lets change working directory to the Python Rename Test folder os.chdir(r'F:\My backup\Documents and Settings\user\My Documents\My Music\The Commitments\The Commitments - Mustang Sally_Recursive\Backup\Python Rename Test') # confirm working directory by printing it out print (os.getcwd()) # loop over the files in the working directory and printing them out for file in os.listdir(os.getcwd()): print (file) This is what I get:

Cue 001 - 002.mp3 ... Cue 001 - 010.mp3 Cue 001 - 0100.mp3 Cue 001 - 0101.mp3 ... How can I get os.listdir to print the files in the proper order?


Источник: https://stackoverflow.com/questions/491 ... s-in-order
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»