[英]How can I order a list of folder names alphabetically using python
I'm trying to print the names of the folders in alphabetical order.我正在尝试按字母顺序打印文件夹的名称。
They obviously are when in the file directory but when I've separated parts of the folder names they won't print in alphabetical order.它们显然是在文件目录中,但是当我分隔部分文件夹名称时,它们不会按字母顺序打印。
This is the code:这是代码:
import os, datetime
from tkinter import Tk
from tkinter.filedialog import askdirectory
SelectDrive = askdirectory(title='Select Folder')
SubjectDetail = os.listdir(SelectDrive)
ListLen = (len(SubjectDetail))
SplitList = [i.split() for i in SubjectDetail]
i = 0
while i < ListLen:
Name = SplitList[-i]
NameLen =len(Name)
DateOfBirth = (Name[NameLen-1])
Name.remove(DateOfBirth)
FullName = ' '.join(Name)
print(FullName," ", DateOfBirth)
i += 1
and the current output:和当前的 output:
Arthur LEWIS 06111984
Sarah Rose WILLIAMS 23091974
Mark THOMAS 11062020
Lewis MCCARTNEY 15021994
and finally the output i'm looking for:最后是我正在寻找的 output:
Arthur LEWIS 06111984
Lewis MCCARTNEY 15021994
Mark THOMAS 11062020
Sarah Rose WILLIAMS 23091974
There is the pathlib library for handling paths.有用于处理路径的pathlib库。 So you could use something like
所以你可以使用类似的东西
from pathlib import Path
from tkinter.filedialog import askdirectory
SelectDrive = askdirectory(title="Select Folder")
path = Path(SelectDrive)
list_of_dirs = sorted([i for i in path.iterdir() if i.is_dir()])
for d in list_of_dirs:
print(d)
Name = SplitList[-i]
is your problem. Name = SplitList[-i]
是你的问题。 When you use a negative index, Python starts counting from the end of the list starting at 1. So if you had a list, a = [1, 2, 3, 4, 5]
, if you do a[-2]
it will count 5
as index -1
then 4
as index -2
and will return that to you.当您使用负索引时, Python 从列表的末尾从 1 开始计数。因此,如果您有一个列表,则
a = [1, 2, 3, 4, 5]
,如果您执行a[-2]
它将5
计为索引-1
,然后将4
计为索引-2
并将其返回给您。 That's why it returns the first file name because -0
is interpreted as index 0, but index -1
means the last element of the list, index -2
would be the second-last element and so on.这就是它返回第一个文件名的原因,因为
-0
被解释为索引 0,但索引-1
表示列表的最后一个元素,索引-2
将是倒数第二个元素,依此类推。
I wrote up some code, it outputs the same thing you requested but I'm not sure it suits your needs:我编写了一些代码,它输出的内容与您要求的相同,但我不确定它是否适合您的需求:
import os
from tkinter.filedialog import askdirectory
SelectDrive = askdirectory(title='Select Folder')
SubjectDetail = os.listdir(SelectDrive)
SplitList = [i.split() for i in SubjectDetail]
for person in SplitList:
print(*person)
If you want to keep your code somewhat the same, you should be able to just use index i
but without adding the negative sign and that should solve your problem: Name = SplitList[i]
.如果你想让你的代码保持不变,你应该能够只使用索引
i
但不添加负号,这应该可以解决你的问题: Name = SplitList[i]
。
Cheers.干杯。
I have change my while loop so it's now looking to see if list length is greater than i.我已经更改了我的 while 循环,所以它现在正在查看列表长度是否大于 i。 This allows the output to be in order.
这使得 output 井然有序。
while ListLen > i:
Name = SplitList[+i]
NameLen =len(Name)
NameSort = []
DateOfBirth = (Name[NameLen-1])
Name.remove(DateOfBirth)
FullName = ' '.join(Name)
print(FullName, " ",DateOfBirth)
i += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.