0

I have this problem where I am trying to organize file names. Unfortunately I had the bright idea to name these image files by a number system so when I output my code "onlyfiles" it comes out as

['0.jpg', '01.jpg', '1.JPG', '10.JPG', '100.JPG', '101.JPG', '102.JPG', '103.JPG', '104.JPG', '105.JPG', '106.JPG', '107.JPG', '108.JPG', '109.JPG', '11.JPG', '110.JPG', '111.JPG', '112.JPG', '113.JPG', '114.JPG', '115.JPG', '116.JPG', '117.JPG', '118.JPG', '119.JPG', '12.JPG', '120.JPG', '121.JPG', '122.JPG', '123.JPG', '124.JPG', '125.JPG', '126.JPG', '127.JPG', '128.JPG', '129.JPG', '13.JPG', '130.JPG', '131.JPG', '132.JPG', '133.JPG', '134.JPG', '135.JPG', '136.JPG', '137.JPG', '138.JPG', '139.JPG', '14.JPG', '140.JPG', '141.JPG', '142.JPG', '143.JPG', '144.JPG', '145.JPG', '146.JPG', '147.JPG', '148.JPG', '149.JPG', '15.JPG', '150.JPG', '151.JPG', '152.JPG', '153.JPG', '154.JPG', '155.JPG', '156.JPG', '157.JPG', '158.JPG', '159.JPG', '16.JPG', '160.JPG', '161.JPG', '162.JPG', '163.JPG', '164.JPG', '165.JPG', '166.JPG', '167.JPG', '168.JPG', '169.JPG', '17.JPG', '170.JPG', '171.JPG', '172.JPG', '173.JPG', '174.JPG', '175.JPG', '176.JPG', '177.JPG', '178.JPG', '179.JPG', '18.JPG', '180.JPG', '181.JPG', '182.JPG', '183.JPG', '184.JPG', '185.JPG', '186.JPG', '187.JPG', '188.JPG', '189.JPG', '19.JPG', '190.JPG', '193.JPG', '194.JPG', '195.JPG', '196.JPG', '197.JPG', '198.JPG', '199.JPG', '2.JPG', '20.JPG', '200.JPG', '201.JPG', '202.JPG', '21.JPG', '22.JPG', '23.JPG', '24.JPG', '25.JPG', '26.JPG', '27.JPG', '28.JPG', '29.JPG', '3.JPG', '30.JPG', '31.JPG', '32.JPG', '33.JPG', '34.JPG', '35.JPG', '36.JPG', '37.JPG', '38.JPG', '39.JPG', '4.JPG', '40.JPG', '41.JPG', '42.JPG', '43.JPG', '44.JPG', '45.JPG', '46.JPG', '47.JPG', '48.JPG', '49.JPG', '5.JPG', '50.JPG', '51.JPG', '52.JPG', '53.JPG', '54.JPG', '55.JPG', '56.JPG', '57.JPG', '58.JPG', '59.JPG', '6.JPG', '60.JPG', '61.JPG', '62.JPG', '63.JPG', '64.JPG', '65.JPG', '66.JPG', '67.JPG', '68.JPG', '69.JPG', '7.JPG', '70.JPG', '71.JPG', '72.JPG', '73.JPG', '74.JPG', '75.JPG', '76.JPG', '77.JPG', '78.JPG', '79.JPG', '8.JPG', '80.JPG', '81.JPG', '82.JPG', '83.JPG', '84.JPG', '85.JPG', '86.JPG', '87.JPG', '88.JPG', '89.JPG', '9.JPG', '90.JPG', '91.JPG', '92.JPG', '93.JPG', '94.JPG', '95.JPG', '96.JPG', '97.JPG', '98.JPG', '99.JPG'

below is my code.

onlyfiles = list([i for i in listdir(r'C:\Users\D&A\Desktop\New folder\snips_library\library') if isfile(join(r'C:\Users\D&A\Desktop\New folder\snips_library\library',i))])

So the question that I am asking how do I organize this properly? I want this to look like 0, 01, 1 , 2 , 3 ,4 ,5 ,6 and so on... any ideas?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Kaelan O'reily
  • 113
  • 1
  • 1
  • 8
  • 2
    since Jim found a duplicate that has a really good answer. I'm withdrawing my answer (which was only modeslty good.) – Dale Wilson Aug 16 '16 at 17:42
  • Thanks for the help I have been looking for 3 days now. I didn't think there was an exact answer on here. But as your previous answer stated to put leading 0's. would that work every time? – Kaelan O'reily Aug 16 '16 at 17:45
  • 1
    See the answers to the questions that Jim references: http://stackoverflow.com/questions/12093940/reading-files-in-a-particular-order-in-python In the accepted answer he shows an example of a custom sort that should display the files in the order you expect. – Dale Wilson Aug 17 '16 at 17:19
  • 1
    The alternative which I mention in my now-deleted message is to rename the files in the file system so they sort correctly. This is not guaranteed behavior, however, so if the order in which the files are displayed is critical you should use the custom-sort method to be certain it's right. – Dale Wilson Aug 17 '16 at 17:20

0 Answers0