SHARE
TWEET

Untitled

a guest Jan 17th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """Rename files or directories to incrementing padded numbers, preserving original order.
  2.  
  3. Takes a directory that looks like this:
  4. diroffiles/
  5. diroffiles//a
  6. diroffiles//b
  7. diroffiles//c
  8. diroffiles//d
  9. diroffiles//e
  10. diroffiles//f
  11. diroffiles//g
  12. diroffiles//h
  13. diroffiles//i
  14. diroffiles//j
  15. diroffiles//k
  16. diroffiles//l
  17. diroffiles//m
  18. diroffiles//n
  19. diroffiles//o
  20. diroffiles//p
  21. diroffiles//q
  22. diroffiles//r
  23. diroffiles//s
  24. diroffiles//t
  25. diroffiles//u
  26. diroffiles//v
  27. diroffiles//w
  28. diroffiles//x
  29. diroffiles//y
  30. diroffiles//z
  31.  
  32. And makes it look like this:
  33. diroffiles//00000
  34. diroffiles//00001
  35. diroffiles//00002
  36. diroffiles//00003
  37. diroffiles//00004
  38. diroffiles//00005
  39. diroffiles//00006
  40. diroffiles//00007
  41. diroffiles//00008
  42. diroffiles//00009
  43. diroffiles//00010
  44. diroffiles//00011
  45. diroffiles//00012
  46. diroffiles//00013
  47. diroffiles//00014
  48. diroffiles//00015
  49. diroffiles//00016
  50. diroffiles//00017
  51. diroffiles//00018
  52. diroffiles//00019
  53. diroffiles//00020
  54. diroffiles//00021
  55. diroffiles//00022
  56. diroffiles//00023
  57. diroffiles//00024
  58. diroffiles//00025
  59.  
  60. """
  61. import argparse
  62. import glob
  63. import os
  64.  
  65. argparser = argparse.ArgumentParser(description="Rename directories to incrementing padded numbers, preserving original order.")
  66. argparser.add_argument("topdir")
  67. cliargs = argparser.parse_args()
  68.  
  69. dir_s = glob.glob(cliargs.topdir + "/*")
  70. dir_s.sort()
  71.  
  72. if len(dir_s) > 0:
  73.     index = 1
  74.     for directory in dir_s:
  75.         os.rename(directory, cliargs.topdir + "/" + "%05d" % index)
  76.         index = index + 1
  77. else:
  78.     logging.error("No directories found in %s" % cliargs.topdir)
  79.     exit(1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top