import os def re_name(path): for file in os.listdir(path): file_path = os.path.join(path, file) # 判斷這個文件是不是文件夾,是文件夾的話就調用本身,把路徑拼接好傳過去 if os.path.isdir(file_path): re_name(file_path) else: # 若是不是文件夾,就開始更名字 if "i.cnblogs.com" in file: file_new = file.replace("i.cnblogs.com", "") file_new_path = os.path.join(path, file_new) os.rename(file_path, file_new_path) if __name__ == '__main__': path = r'F:\BaiduNetdiskDownload\' re_name(path)