参考链接: 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
干扰
>>>
>>>
>>>
本文通过Python的os模块演示了如何使用os.path.split和os.path.splitext进行文件路径的拆分。展示了如何获取文件的目录和文件名,以及如何分离文件扩展名。
669

被折叠的 条评论
为什么被折叠?



