Skip to content

Instantly share code, notes, and snippets.

@jasonbot
Created September 11, 2023 20:30
Show Gist options
  • Save jasonbot/d64b5f5ed0eb8df72109d26d5c3b8421 to your computer and use it in GitHub Desktop.
Save jasonbot/d64b5f5ed0eb8df72109d26d5c3b8421 to your computer and use it in GitHub Desktop.
Make Files NTFS-Safe
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
os.rename(file, r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment