#! /home/phd/.local/bin/python3 import os from recode_filenames import parse_args, build_recode from_encoding, to_encoding, dirnames = parse_args(default='.') _recode = build_recode(from_encoding, to_encoding) def _onerror(exc): raise exc save_dir = os.getcwd() for start_dir in dirnames: for dirname, _subdirs, fnames in list( os.walk(start_dir, topdown=False, onerror=_onerror) ): if dirname != '.': os.chdir(dirname) for filename in fnames: # if not exists - it was renamed already if os.path.exists(filename) and \ os.path.isfile(filename): newname = _recode(filename) if not isinstance(newname, str): newname = newname.decode() if newname != filename: os.rename(filename, newname) if dirname != '.': os.chdir('..') dirname = os.path.basename(dirname) newname = _recode(dirname) if not isinstance(newname, str): newname = newname.decode() if newname != dirname: os.rename(dirname, newname) os.chdir(save_dir)