Как заставить os.listdir перечислять файлы по порядку [дубликат] ⇐ Python
Как заставить os.listdir перечислять файлы по порядку [дубликат]
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
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
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Python os listdir перечисляет файлы для неправильного каталога [дубликат]
Anonymous » » в форуме Python - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Python os listdir перечисляет файлы для неправильного каталога [дубликат]
Anonymous » » в форуме Python - 0 Ответы
- 14 Просмотры
-
Последнее сообщение Anonymous
-