Created
November 24, 2020 19:37
-
-
Save VijayS1/2881cc3fffa6a5ee0c8564e3004749b0 to your computer and use it in GitHub Desktop.
A script to find and rename filenames containing unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import sys, os, unicodedata | |
def main(argv): | |
if len(argv) != 2: | |
raise Exception('Syntax: FindUnicodeFiles.py <directory>') | |
startdir = argv[1] | |
if not os.path.isdir(startdir): | |
raise Exception('"%s" is not a directory' % startdir) | |
for r in recurse_breadth_first(startdir, is_unicode_filename): | |
print(r.encode('unicode-escape').decode('ascii')) | |
nn = unicodedata.normalize('NFKD',r).encode('ASCII','ignore').decode() | |
print('-->',nn) | |
if(yesno("Rename File")): | |
os.rename(r, nn) | |
def yesno(question): | |
"""Simple Yes/No Function.""" | |
prompt = f'{question} ? (y/n): ' | |
ans = input(prompt).strip().lower() | |
if ans not in ['y', 'n']: | |
print(f'{ans} is invalid, please try again...') | |
return yesno(question) | |
if ans == 'y': | |
return True | |
return False | |
def recurse_breadth_first(dirpath, test_func): | |
namesandpaths = [(f, os.path.join(dirpath, f)) for f in os.listdir(dirpath)] | |
for (name, path) in namesandpaths: | |
if test_func(name): | |
yield path | |
for (_, path) in namesandpaths: | |
if os.path.isdir(path): | |
for r in recurse_breadth_first(path, test_func): | |
yield r | |
def is_unicode_filename(filename): | |
return any(ord(c) >= 0x7F for c in filename) | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment