参考链接: os.path.split(path)
参考链接: os.path.splitext(path)
代码实验展示:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import os
>>> myPath = r'C:\Users\chenxuqi\Desktop\干扰'
>>> myPath = r"C:\Users\chenxuqi\Desktop\干扰\对抗样本\165.bmp"
>>> os.path.splitext(myPath)
('C:\\Users\\chenxuqi\\Desktop\\干扰\\对抗样本\\165', '.bmp')
>>> os.path.split(myPath)
('C:\\Users\\chenxuqi\\Desktop\\干扰\\对抗样本', '165.bmp')
>>>
>>> result = os.path.splitext(myPath)
>>> print(*result)
C:\Users\chenxuqi\Desktop\干扰\对抗样本\165 .bmp
>>> print(*result,sep='\n')
C:\Users\chenxuqi\Desktop\干扰\对抗样本\165
.bmp
>>>
>>> result = os.path.split(myPath)
>>> result
('C:\\Users\\chenxuqi\\Desktop\\干扰\\对抗样本', '165.bmp')
>>> print(*result,sep='\n')
C:\Users\chenxuqi\Desktop\干扰\对抗样本
165.bmp
>>>
>>>
>>> myPath = r'C:\Users\chenxuqi\Desktop\干扰'
>>> result = os.path.splitext(myPath)
>>> print(*result,sep='\n')
C:\Users\chenxuqi\Desktop\干扰
>>> result
('C:\\Users\\chenxuqi\\Desktop\\干扰', '')
>>>
>>> result = os.path.split(myPath)
>>> result
('C:\\Users\\chenxuqi\\Desktop', '干扰')
>>> print(*result,sep='\n')
C:\Users\chenxuqi\Desktop
干扰
>>>
>>>
>>>