10

I am working with a program that writes output to a csv file based on the order that files are read in from a directory. However with a large number of files with the endings 1,2,3,4,5,6,7,8,9,10,11,12. My program actually reads the files by I guess alphabetical ordering: 1,10,11,12....,2,20,21.....99. The problem is that another program assumes that the ordering is in numerical ordering, and skews the graph results.

The actually file looks like: String.ext.ext2.1.txt, String.ext.ext2.2.txt, and so on...

How can I do this with a python script?

Jim
  • 3,236
  • 8
  • 33
  • 43

4 Answers4

17
files = ['String.ext.ext2.1.txt', 'String.ext.ext2.12.txt', 'String.ext.ext2.2.txt']
# files: coming from os.listdir() sorted alphabetically, thus not numerically

sorted_files = sorted(files, key=lambda x: int(x.split('.')[3]))
# returns: ['String.ext.ext2.1.txt', 'String.ext.ext2.2.txt', 'String.ext.ext2.12.txt']
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 2
    `sorted_files = sorted(files, key=lambda x: int(x.split('.')[3]))` The `sorted` functions sorts it's first argument using a selector given by the second argument. Think about sorting a table by a certain column. Now, for the second argument. It is a function transforming your filename to a list of parts (separated by `.`) and taking the 3rd element in that list – Mihai Maruseac Jul 19 '11 at 07:55
  • 1
    Got it to work with this: sorted_files = sorted(file_list, key=lambda x: int(x.split('.')[3]) if (x.endswith("txt")) else x) – Jim Jul 19 '11 at 08:01
5

Sort your list of files in the program. Don't rely on operating system calls to give the files in the right order, it depends on the actual file system being used.

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
5
  1. Use os.listdir to get a list of file names.
  2. Sort the list using natural sort order.
  3. Process the files in order of your sorted list.
ʇsәɹoɈ
  • 22,757
  • 7
  • 55
  • 61
2

You can use something like this:

fileNames = ['String.ext.ext2.2.txt', 'String.ext.ext2.20.txt', 'String.ext.ext2.1.txt', 'String.ext.ext2.10.txt', 'String.ext.ext2.11.txt', 'String.ext.ext2.0.txt',]
fileNames = sorted(fileNames, key=lambda y: int(y.rsplit('.', 2)[1]))
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52