s=r"C:\Users\Desktop\lesson\python\calss1.py"
s.split("\\")[-1] #输出为 class1.py
方法二:rfind
s=r"C:\Users\Desktop\lesson\python\calss1.py"
n=s.rfind("\\")#找到"\\"出现的位置
s[n+1:] #输出为 class1.py
s[:n] #输出为 'C:\\Users\\Desktop\\lesson\\python'
方法三:os.path.basename()
s='C:/Users/Desktop/lesson/python/calss1.py'
import os
os.path.dirname(s) #输出为 'C:/Users/Desktop/lesson/python'
os.path.basename(s) #输出为 class1.py
————————————————
原文链接:https://blog.csdn.net/weixin_44034578/article/details/114992878