[Tutor] Question about my reading and writing files homework
Eryk Sun
eryksun at gmail.com
Sat Sep 26 18:22:50 EDT 2020
On 9/25/20, Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>
> If, OTOH, you are expected to use os.path, then yes -- you will need to
> save the base path, maybe sort the file and directory lists, etc. (pathlib
> seems to return a sorted list, even though the documentation says
> "arbitrary order").
pathlib.Path.iterdir just iterates the result of os.listdir to yield
Path items instead of strings. os.listdir does not sort the result
from the OS API.
At the Windows API level, FindFirstFileW and FindNextFileW do not sort
the result from the NT system call, NtQueryDirectoryFileEx, which in
turn does not sort the result from the filesystem request,
IRP_MJ_DIRECTORY_CONTROL. Sorting the result wouldn't reasonably scale
to arbitrarily large directories at the API and system call levels,
and, even if implemented, applications would usually want a different
sort order, based on different criteria and comparisons.
If the listing appears to be sorted, then it's just how the filesystem
stores the directory listing on disk. For example, NTFS stores a
directory as a b-tree name index, ordered by comparing corresponding
characters in two names by ordinal value (i.e. a locale-unaware,
non-natural sort). FAT filesystems, on the other hand, store a
directory as an unsorted list of names. A new name in the directory
takes the first available slot. At first, each new filename is simply
appended to the end of the list, but deleting a file leaves an open
slot for a new entry.
More information about the Tutor
mailing list