0

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:

Arthur LEWIS 06111984
Sarah Rose WILLIAMS 23091974
Mark THOMAS 11062020
Lewis MCCARTNEY 15021994

and finally the output i'm looking for:

Arthur LEWIS 06111984
Lewis MCCARTNEY 15021994
Mark THOMAS 11062020
Sarah Rose WILLIAMS 23091974
nellie456
  • 57
  • 1
  • 5
  • 1
    Checkout the sort method – Employee Jun 18 '20 at 12:23
  • You can easily print all the info for a person by printing an unpacked version of it, `print(*SplitList[i])`. In Python, `*` unpacks a list. This would be the same as writing `print(SplitList[i][0], splitList[i][1], splitList[i][2], ...)`. – BOB Jun 18 '20 at 12:41

3 Answers3

0

There is the pathlib library for handling paths. 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)
cad106uk
  • 471
  • 5
  • 5
  • I don't think reading the files in order is the problem. `os.listdir()` seems to return them in alphabetical order. – BOB Jun 18 '20 at 12:44
0

Name = SplitList[-i] is your problem. 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. 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.

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

Cheers.

BOB
  • 103
  • 1
  • 6
  • If you want to learn more about the slice notation, check out this link: https://stackoverflow.com/questions/509211/understanding-slice-notation. – BOB Jun 18 '20 at 13:14
  • BOB, thanks for your answer, while it does give the correct result i probably should of mentioned i'm then going to insert the name and then numbers into a spreadsheet so would be of use to have then each in a variable. – nellie456 Jun 18 '20 at 13:29
  • In that case, if each person has the same type of information, eg. name, age..., then you should still be able to use the for loop but hardcode writing to the spreadsheet. For example, in the for loop you would write `person[0]` to that column in the spreadsheet at the correct row. If you need access to the row number, you can do something like this: `for index, person in enumerate(SplitList):` where index will start at zero and count upwards and person will be the data for each person. What are you using to write to the spreadsheet? – BOB Jun 18 '20 at 13:45
0

I have change my while loop so it's now looking to see if list length is greater than i. This allows the output to be in order.

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
nellie456
  • 57
  • 1
  • 5