文件名处理

如何从路径中获取路径、后缀、文件名

import os
s = r"C:\Program Files\JetBrains\PyCharm 2017.3\helpers\pydev\pydevconsole.py"

os.path.split(s)    //路径和文件全名
('C:\\Program Files\\JetBrains\\PyCharm 2017.3\\helpers\\pydev', 'pydevconsole.py')

os.path.splitext('pydevconsole.py')
 ('pydevconsole', '.py')

os.path.basename(s) 
 'pydevconsole.py'

os.path.basename('pydevconsole.py')
'pydevconsole.py'

os.path.dirname(s)
 'C:\\Program Files\\JetBrains\\PyCharm 2017.3\\helpers\\pydev'

os.path.splitext(s)   //获取后缀
('C:\\Program Files\\JetBrains\\PyCharm 2017.3\\helpers\\pydev\\pydevconsole', '.py')

os.path.splitext(os.path.split(s)[1])    //获取文件名和后缀
 ('pydevconsole', '.py')

注意:

os.path.dirname('pydevconsole.py')
 ''

os.path.split('pydevconsole.py')
 ('', 'pydevconsole.py')

你可能感兴趣的:(文件名处理)