Last active
July 10, 2024 22:00
-
-
Save jasonbot/d64b5f5ed0eb8df72109d26d5c3b8421 to your computer and use it in GitHub Desktop.
Make Files NTFS-Safe
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
import os | |
import pathlib | |
REPLACE_CHARS = ':*?"' | |
def walk(path: pathlib.Path): | |
for item in path.iterdir(): | |
if item.is_dir(): | |
yield from walk(item) | |
elif item.is_file(): | |
yield item | |
def need_rename(filename: pathlib.Path): | |
if any(c in str(filename) for c in REPLACE_CHARS): | |
for c in REPLACE_CHARS: | |
filename = str(filename).replace(c, "_") | |
np = pathlib.Path(filename) | |
return np | |
p = pathlib.Path('.') | |
for file in walk(p): | |
if r := need_rename(file): | |
print(file, '->', r) | |
try: | |
os.makedirs(r.parent) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment