1

The title says it all. Its pretty easy to do it normally, but it has to maintain the original order from the folder. I've got some files (1000 all told) that I've got to put into a .csv. The filenames are something like: File names example

I've been banging my head into the wall.

Timus
  • 10,974
  • 5
  • 14
  • 28
Lamb Sauce
  • 81
  • 9
  • The files order in a directory depends on the underlying filesystem and it's not something really reliable (https://unix.stackexchange.com/questions/13451/what-is-the-directory-order-of-files-in-a-directory-used-by-ls-u) you better specify an order and sort the files in python directly. – luxcem May 09 '23 at 13:06
  • There's really no such thing as "original order". Maybe you'd find a [natural sort](https://stackoverflow.com/q/4836710/5987) by filename helpful. – Mark Ransom May 09 '23 at 13:14
  • @MarkRansom: I disagree, since there is a "original order", which is the sequence, in which `ls`under Linux lists the files. I agree, that this of no practical use and sombody publishing a screenshot is unlikely to refer to that order. – guidot May 09 '23 at 13:18
  • @guidot and if you delete a file from the middle of the list and then create a new one, where does the new file show up? If you need a list sorted by creation date/time, it's better to be explicit about it. – Mark Ransom May 09 '23 at 13:21
  • @guidot That's incorrect; `ls` sorts its output. However, e.g. `find` outputs files in the physical order they are stored on the disk. – tripleee May 09 '23 at 13:55
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee May 09 '23 at 13:55

2 Answers2

2

os.scandir() returns the files in whichever order the OS returns them. This is the closest you can get to "original order" if you mean the order they are stored in the directory.

import os
import csv

with open('file.csv', 'w') as output:
    writer = csv.writer(output)
    for file in os.scandir():
        writer.writerow([file.name])

Using a CSV file for a simple single column of text is slightly silly, but perhaps your users are unsophisticated (or perhaps your file names are really sophisticated, and you need to correctly cope with file names with newlines in them etc).

Your picture looks like the files are simply in alphabetical order, which is how some file browsers will display them. os.listdir() will produce the files in alphabetical order, or just apply sorted() to the list you got from os.scandir() if you want to apply some sort of custom sort order, such as by creation date (if your OS provides that) or latest modification date. (The objects returned by os.scandir have a .stat member which contains this information.)

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

I've managed to solve the problem by simply renaming all the files to 000, 001, 002 etc. Then when I alphanumerically sort the files, it makes everything better. I think that the sort function from the python library is slightly different to the algorithm used in MS Windows. Thank you tripleee!

Lamb Sauce
  • 81
  • 9
  • 1
    The sort order used by Python is strictly alphanumeric unless you use a `key` argument. Windows uses something similar to a natural sort as I mentioned in the comments. – Mark Ransom May 12 '23 at 17:34