Skip to content

Instantly share code, notes, and snippets.

@olliefr
Created August 1, 2020 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olliefr/ca61bf23e586e5475458c0b3530739e4 to your computer and use it in GitHub Desktop.
Save olliefr/ca61bf23e586e5475458c0b3530739e4 to your computer and use it in GitHub Desktop.
Recode file names in the current directory from CP-1251 to file system's native encoding using Python
# -*- coding: utf-8 -*-
"""
This script was written to solve a specific problem:
A large directory of video files had file names encoded in Windows-1251.
These file names would not display correctly in Windows 10 or OS X.
I wanted to recode them to the native encoding for the current file system.
Python is smart enough to apply the correct encoding when writing out
the file names to the file system.
The files must be in the 'current' directory. The extension 'wmv' is assumed.
@author: Oliver Frolovs
@year: 2018
"""
from os import listdir, rename
from fnmatch import fnmatch
for old_file_name in listdir('.'):
if fnmatch(old_file_name, '*.wmv'):
new_file_name = bytes(old_file_name, 'latin1').decode('cp1251')
try:
rename(old_file_name, new_file_name)
print('Renamed "{}" to "{}"'.format(old_file_name, new_file_name))
except:
print("File skipped because failed to rename: {}".format(old_file_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment