Guest User

Untitled

a guest
Oct 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os, sys
  4. import unicodedata
  5.  
  6. def depthfirstwalk(topdir):
  7.     if not os.path.isdir(topdir):
  8.         yield topdir
  9.         return
  10.  
  11.     for subpath in os.listdir(topdir):
  12.         fullpath = os.path.join(topdir, subpath)
  13.         if os.path.isdir(fullpath):
  14.             for derived in depthfirstwalk(fullpath):
  15.                 yield derived
  16.         yield fullpath
  17.  
  18. #os.path.walk(sys.argv[1], fswalk, 1)
  19. #os.path.walk(sys.argv[1], fswalk, 0)
  20. for path in depthfirstwalk(sys.argv[1]):
  21.     basename = os.path.basename(path)
  22.     try:
  23.         basename.decode('ascii')
  24.     except UnicodeError:
  25.         try:
  26.             #utfname = name.decode('utf-8').encode('cp949')
  27.             #utfname = name.decode('cp949').encode('utf-8')
  28.             utfname = unicodedata.normalize('NFC', basename.decode('utf-8')).encode('utf-8')
  29.             if utfname != basename:
  30.                 dirname = os.path.dirname(path)
  31.                 newname = os.path.join(dirname, utfname)
  32.                 print newname
  33.                 os.rename(path, newname)
  34.         except UnicodeError:
  35.             pass
  36.  
  37. # ex: ts=8 sts=4 et
Add Comment
Please, Sign In to add comment