import os new_ordered_filenames = [ "01. 3rd Award - International Astronomy Olympiad 2007.pdf", "02. 1st Place - National Astronomy Olympiad 2007.pdf", "03. 1st Place - National Astronomy Olympiad 2009.pdf", "04. Medals Various - 3rd place International Astronomy Olympiad, 1st Place Italian-Bulgarian Physics Competition, 2x 1st Place + 2nd Place National Astronomy Olympiad, SoftUni Tech Fest Hackathon.jpg", "05. Physicist Of The Class Award, High School 2006-2011.jpg", "06. Statue - 1st Place - Multimedia Award, National Information Technology Award, 2010.jpg", "07. Statue - 2nd Place Bulgaria Italy 2010.jpg", "08. 2nd Place - Bulgarian-Italian Physics Competition 2010.pdf", "10. 2nd Place - National Astronomy Olympiad 2008.pdf", "09. 2nd Place - Award by the Network of Innovators Teachers - Video competition, 2010.pdf", "14. 3rd Place - National Autumn Physics Competition 2008.pdf", "11. 3rd Award - National Autumn Physics Competition 2009.pdf", "12. 3rd Award - National Spring Physics Competition 2009.pdf", "13. 3rd Place - City round of National Physics Competition 2008.pdf", "43. University of Glasgow, Class Representative Certificate 2012.pdf", "35. Certificate of Achievement - Google Android Camp 2012.pdf", "15. Best Observation Round - National Astronomy Olympiad 2009.pdf", "16. Certificate - European Expo Sciences Moscow 2010.pdf", "21. Certificate - For young amateur astronomer 2010.pdf", "37. Certificate - Participation in Astronomy, Astrophysics and Informatics Summer School 2008.pdf", "38. Certificate - Participation in Astronomy, Astrophysics and Informatics Summer School 2009.pdf", "39. Certificate - Participation in Astronomy, Astrophysics and Informatics Summer School 2010.pdf", "29. Certificate - National Olympiad IT 2010.pdf", "41. Certificate - Young Talents 2009.pdf", "42. Certificate - Young Talents 2010.pdf", "44. Special Award - For Preparing a movie Biological effects of the Ionized Radiation, 2010.pdf", "45. Work - Work at Ministry of Finances 2012.pdf", "22. Certificate - National Astronomy Olympiad 2008.pdf", "23. Certificate - National Astronomy Olympiad 2009.pdf", "24. Certificate - National Astronomy Olympiad 2010.pdf", "25. Certificate - National Astronomy Olympiad, Qualification Round 2007.pdf", "26. Certificate - National Astronomy Olympiad, Qualification Round 2008.pdf", "27. Certificate - National Astronomy Olympiad, Qualification Round 2009.pdf", "28. Certificate - National Astronomy Olympiad, Third Round 2007.pdf", "30. Certificate - National Physics Conference 2010.pdf", "31. Certificate - National Physics Olympiad 2009.pdf", "32. Certificate - National Physics Olympiad 2010.pdf", "33. Certificate - National Spring Physics Competition 2008.pdf", "34. Certificate - National Spring Physics Competition 2010.pdf", "40. Certificate - Plovdiv University acceptance 2009.pdf", "17. Certificate - For achievements in biology olympiad 2006.pdf", "18. Certificate - For high grades achievements 2006.pdf", "19. Certificate - For high mathematics achievements 2005.pdf", "20. Certificate - For high sports achievements 2006.pdf", "36. Certificate of Education in English as a Foreign Language 2009.pdf", ] def main(): """ rename all files in directory /home/pi2/Desktop/application/awards of type "NN. filename.ext" where NN. is a number with a leading 0 (e.g. 01. or 05.). However some of the numbers are repeated. I want to rename all files and have unique number for each file. """ filenames = os.listdir("/home/pi2/Desktop/application/awards") ordered_filenames = sorted(filenames) new_order = {} for idx, filename in enumerate(ordered_filenames): if filename in ['new']: continue new_idx = new_ordered_filenames.index(filename) new_filename = "{:02d}.{}".format(new_idx + 1, '.'.join(filename.split(".")[-2:])) new_order[filename] = new_filename for old_filename, filename in new_order.items(): # copy os.system("cp '/home/pi2/Desktop/application/awards/{}' '/home/pi2/Desktop/application/awards/new/{}'".format(old_filename, filename)) if __name__ == "__main__": main()