I applied @jasonharper's comment:
The questionmark wasn't the problem, instead I tried changed dirnames while iterating through filenames and changing them.
Working code:
import os
import re
def changefilename():
dir_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..",".."))
for root, dirs, files in os.walk(dir_path):
for dir in dirs:
new_dir = re.sub(r"([^\?]*)\?(.*)", r"\1\2", dir)
print(dir, root, new_root)
os.rename(os.path.join(root, dir),os.path.join(root, new_dir))
changefilename()