简体   繁体   English

如何使用 python 按字母顺序排列文件夹名称列表

[英]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.

相关问题 如何在 python/mako 中按字母顺序排列列表 - How to order a list alphabetically in python/mako Python数据框:如何获取按字母顺序排列的列名列表 - Python Dataframe: How to get alphabetically ordered list of column names 如何按字母顺序排列对象列表? - How to order a list of objects alphabetically? Python:如何按字母顺序对文本文件中的名称进行排序? - Python: How do i sort Names in a text file alphabetically? 提取目录中的文件夹名称列表后,如何使用 Python 将它们重命名为另一种格式? - After extracting the list of folder names in a directory, how do I rename them in another format using Python? Python:如何获取带有名称列表、两列格式的导入文本文件,并将它们放入按字母顺序排序的 Python 中? - Python: How do I take an imported text file with a list of names, two column format, and put them in python sorted alphabetically? 使用Python中的函数列出文件夹名称 - Using function in Python to list folder names 如何根据第二个或第三个单词或类似的东西在python中按字母顺序对句子列表进行排序 - How can I sort a list for sentence alphabetically in python based on 2nd or 3rd word or something like that 我将如何在 Python 中使用基数排序按字母顺序对(很长的)对象列表进行排序? - How would I alphabetically sort a (very long) list of objects using radix sort in Python? 在将项目添加到列表之前,如何按字母顺序对其进行排序? - How can I sort an item alphabetically before I add it to the list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM