Skip to content

Instantly share code, notes, and snippets.

@vjandrea
Last active May 20, 2019
Embed
What would you like to do?
Correct the filenames of files extracted in Windows from archives compressed on Mac OS
import glob
import re
import os
path = 'puntata*/**/BLOCCO*/*.mp3'
def repair_filename(filename):
repair = {
'´╝ê': '(', # U+0028 LEFT PARENTHESIS
'´╝ë': ')', # U+0029 RIGHT PARENTHESIS
'A╠ê': 'Ä', # U+00C4 LATIN CAPITAL LETTER A WITH DIAERESIS
'a╠ü': 'á', # U+00E1 LATIN SMALL LETTER A WITH ACUTE
'a╠é': 'â', # U+00E2 LATIN SMALL LETTER A WITH CIRCUMFLEX
'a╠â': 'ã', # U+00E3 LATIN SMALL LETTER A WITH TILDE
'a╠ê': 'ä', # U+00E4 LATIN SMALL LETTER A WITH DIAERESIS
'e╠ü': 'é', # U+00E9 LATIN SMALL LETTER E WITH ACUTE
'A╠â': 'ñ', # U+00F1 LATIN SMALL LETTER N WITH TILDE
'o╠ü': 'ó', # U+00F3 LATIN SMALL LETTER O WITH ACUTE
' ├©': 'ø', # U+00F8 LATIN SMALL LETTER O WITH STROKE
'u╠ü': 'ú', # U+00FA LATIN SMALL LETTER U WITH ACUTE
'u╠ê': 'ü', # U+00FC LATIN SMALL LETTER U WITH DIAERESIS
'─▒ ': 'ı', # U+0131 LATIN SMALL LETTER DOTLESS I
's╠º': 'ş', # U+015F LATIN SMALL LETTER S WITH CEDILLA
}
for match, replace in repair.items():
# print(match, replace)
filename = re.sub(match, replace, filename)
return filename
dir_scan = glob.glob(path)
for filename in dir_scan:
if re.search('╠|╝|▒', filename):
new_filename = repair_filename(filename)
print('RENAMING ', filename, ' AS ', new_filename)
os.rename(filename, new_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment