Skip to content

Instantly share code, notes, and snippets.

@Mr-Z-2697
Last active November 5, 2021 21:41
Show Gist options
  • Save Mr-Z-2697/0cf9ee456651ca272b390e9fbf25e716 to your computer and use it in GitHub Desktop.
Save Mr-Z-2697/0cf9ee456651ca272b390e9fbf25e716 to your computer and use it in GitHub Desktop.
repair filename decoded by incorrect codepage
import os,sys
import pathlib
def ren(x,dryrun):
oripath=pathlib.Path(x).absolute()
oriname=oripath.name
modname=oriname.encode('cp936').decode('cp932')
modpath='\\'.join(str(oripath).split('\\')[:-1]+[modname]) # for windows, this should be modified to work on linux
if not dryrun:
os.rename(oripath,modpath)
else:
print('rename \"%s\" to \"%s\"\n'%(oripath,modpath))
for x in sys.argv[1:]:
ren(x,True)
ex=int(input('\nfile will be renamed according to above, execute? (1/0): '))
if ex:
for x in sys.argv[1:]:
ren(x,False)
input('%s, enter to exit'%('complete' if ex else 'done nothing'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment