5

I have files with a number appended at the end e.g:

  file_01.csv
  file_02.csv
  file_03.csv

I am looking for a simple way of identifying the file with the largest number appended to it. Is there a moderately simple way of achieving this? ... I was thinking of importing all file names in the folder, extracting the last digits, converting to number, and then looking for max number, however that seems moderately complicated for what I assume is a relatively common task.

kyrenia
  • 5,431
  • 9
  • 63
  • 93
  • So you want to read all file names from a directory and get the filename having largest number in its name ? – Tanveer Alam Nov 30 '14 at 20:39
  • yes, so in this case, i would want to know that it is "file_03.csv" (the part of the "file_" is consistent every time) – kyrenia Nov 30 '14 at 20:40

2 Answers2

10

if the filenames are really formatted in such a nice way, then you can simply use max:

>>> max(['file_01.csv', 'file_02.csv', 'file_03.csv'])
'file_03.csv'

but note that:

>>> 'file_5.csv' > 'file_23.csv'
True
>>> 'my_file_01' > 'file_123'
True
>>> 'fyle_01' > 'file_42'
True

so you might want to add some kind of validation to your function, and/or or use glob.glob:

>>> max(glob.glob('/tmp/file_??'))
'/tmp/file_03'
ch3ka
  • 11,792
  • 4
  • 31
  • 28
0
import re
x=["file_01.csv","file_02.csv","file_03.csv"]
print max(x,key=lambda x:re.split(r"_|\.",x)[1])
vks
  • 67,027
  • 10
  • 91
  • 124