from os import listdir, rename from os.path import isfile, isdir, join def scroll_dir(dpath): children = [c for c in listdir(dpath)] for c in children: p = join(dpath, c) if isdir(p): scroll_dir(p) elif isfile(p): change_file_name(p) def change_file_name(fpath): newname = str(fpath)\ .replace(startpath, "")\ .lstrip("/")\ .replace("/", "__") newname = join(startpath, newname) rename(fpath, newname) print(f'{fpath} -> {newname}') if __name__ == "__main__": print("/base/dir/subdir/file.mp4 -> /base/dir__subdir__file.mp4") startpath = input("base dir path: ") scroll_dir(startpath)