OS模块,重命名文件和文件夹。并学会使用try异常语句(注意缩进)
在编辑路径时,"\\"="/"
# -*- coding: UTF-8 -*- import os path = "C:/Users/Administrator/Desktop/A爱B.xlsx" newname = "C:\\Users\\Administrator\\Desktop\\B爱A.xlsx" try: os.rename(path,newname) except IOError: print("Error: 错了") else: print("已处理")