当需要使用路径名来获取文件名、目录名、绝对路径等等的时候,墙裂推荐使用常用路径操作模块os.path。
Talk is cheap. Show me the code.
__author__ = '1665407674@qq.com'
# -*- coding: utf-8 -*
import os
root_path = 'C:\\Python37\\Tools\\scripts\\2to3.py'
# 杩斿洖鏂囦欢鍚?
basename = os.path.basename(root_path)
print(basename)#2to3.py
# 杩斿洖鐩綍璺緞
dirname = os.path.dirname(root_path)
print(dirname)#C:\\Python37\\Tools\\scripts
# 鍒嗗壊鏂囦欢鍚嶄笌璺緞
dirname,basename = os.path.split(root_path)
print(dirname)#C:\Python37\Tools\scripts
print(basename)#2to3.py
# 鍒嗗壊鏂囦欢鍚嶄笌璺緞
root,ext = os.path.splitext(root_path)
print(root)#C:\Python37\Tools\scripts\2to3
print(ext)#.py
# 灏嗙洰褰曞拰鏂囦欢鍚嶅悎鎴愪竴涓矾寰?
join_path = os.path.join('C:\Python37\Tools','scripts','2to3.py')
print( join_path ) #'C:\\Python37\\Tools\\scripts\\2to3.py'
#绫籙nix鐨勮矾寰?
root_path = '/Users/Google/