Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Created October 17, 2022 10:52
Show Gist options
  • Save kuanyui/1b691a07bf54d1ad3b606193e51a8e70 to your computer and use it in GitHub Desktop.
Save kuanyui/1b691a07bf54d1ad3b606193e51a8e70 to your computer and use it in GitHub Desktop.
To rename files in ext4 to exFAT. Forked from https://askubuntu.com/a/671466/442465
#!/usr/bin/env python3
import os
import shutil
import sys
if len(sys.argv) == 1:
print('[USAGE] Please provide directory in argv.')
exit(1)
directory = sys.argv[1]
# --- set replacement below in the format ("<string>", "<replacement>") as below
REPLACE_MAP = [
("<", "<"),
(">", ">"),
(":", ":"),
('"', "”"),
("/", "/"),
("\\", "\"),
("|", "|"),
("?", "?"),
("!", "!"),
("*", "*"),
]
# ---
for root, dirs, fileNames in os.walk(directory):
for oldFileName in fileNames:
newFileName = oldFileName
for c in REPLACE_MAP:
newFileName = newFileName.replace(c[0], c[1])
if newFileName != oldFileName:
tmpFileName = newFileName; n = 0
while os.path.exists(root+"/"+newFileName):
n = n+1; newFileName = "__duplicated__" + str(n) + "_" + tmpFileName
oldFilePath = os.path.join(root, oldFileName)
newFilePath = os.path.join(root, newFileName)
print('[old]-->', oldFilePath)
print('[new]<--', newFilePath)
shutil.move(oldFilePath, newFilePath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment