import os image_dir = "./JPEGImages" xml_dir = "./Annotations" txt_dir = "./labels" buff_dir = txt_dir for files_name in os.listdir(buff_dir): if len(files_name.split(" ")) >1: os.rename(os.path.join(buff_dir,files_name), os.path.join(buff_dir,files_name.replace(" ","_"))) print(os.path.join(buff_dir,files_name.replace(" ","_")))