Last active
April 26, 2021 16:16
-
-
Save Cologler/c1862595bdb0d13c900a34a5e93f9fe4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
# | |
# Copyright (c) 2021~2999 - Cologler <[email protected]> | |
# ---------- | |
# check if there has some invisible chars in your file path. | |
# ---------- | |
from typing import * | |
import os | |
import sys | |
spchs = [ | |
'\u200e', # LEFT-TO-RIGHT MARK | |
'\u200f', # RIGHT-TO-LEFT MARK | |
] | |
def iter_match(root: str, depth: int): | |
if os.path.isdir(root): | |
try: | |
childs = os.listdir(root) | |
except (PermissionError, FileNotFoundError): | |
pass | |
else: | |
for name in childs: | |
child_path = os.path.join(root, name) | |
if os.path.isdir(child_path) and depth > 0: | |
yield from iter_match(child_path, depth-1) | |
for spch in spchs: | |
if spch in name: | |
yield child_path | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
args = argv[1:] | |
depth = 1000000 if '-r' in args else 0 | |
if depth > 0: | |
args.remove('-r') | |
for item in args: | |
r = list(iter_match(item, depth)) | |
if r: | |
print('Some path contains invisible chars:') | |
for match in reversed(r): | |
print(f' {match!r}') | |
if __name__ == '__main__': | |
main() | |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
# | |
# Copyright (c) 2021~2999 - Cologler <[email protected]> | |
# ---------- | |
# normalize file path to remove all invisible chars. | |
# ---------- | |
from typing import * | |
import os | |
import sys | |
spchs = [ | |
'\u200e', # LEFT-TO-RIGHT MARK | |
'\u200f', # RIGHT-TO-LEFT MARK | |
] | |
def iter_match(root: str, depth: int): | |
if os.path.isdir(root): | |
try: | |
childs = os.listdir(root) | |
except (PermissionError, FileNotFoundError): | |
pass | |
else: | |
for name in childs: | |
child_path = os.path.join(root, name) | |
if os.path.isdir(child_path) and depth > 0: | |
yield from iter_match(child_path, depth-1) | |
for spch in spchs: | |
if spch in name: | |
yield child_path | |
def normalize(name): | |
for spch in spchs: | |
name = name.replace(spch, '') | |
return name | |
def rename_item(base_dir, name): | |
new_name = normalize(name) | |
if new_name != name: | |
old_path = os.path.join(base_dir, name) | |
new_path = os.path.join(base_dir, new_name) | |
print(' %r\n -> %r' % (old_path, new_path)) | |
try: | |
os.rename(old_path, new_path) | |
except (PermissionError, FileNotFoundError): | |
print(' × Failed to rename.') | |
def main(argv=None): | |
if argv is None: | |
argv = sys.argv | |
args = argv[1:] | |
depth = 1000000 if '-r' in args else 0 | |
if depth > 0: | |
args.remove('-r') | |
for item in args: | |
for match in iter_match(item, depth): | |
rename_item(*os.path.split(match)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment