realpath
返回指定文件的标准路径,而非软链接所在的路径
import os
real_path = os.path.realpath(__file__)
print('脚本的真实路径:',real_path )
print('..', os.path.realpath('..'))
脚本的标准路径: D:\python_file\notebook\python开发\djangoHelloWord\test.py
… D:\python_file\notebook\python开发
abspath
返回指定文件的绝对路径
import os
abs_path = os.path.abspath(__file__)
print('脚本的绝对路径:',abs_path)
print('..', os.path.abspath('..'))
脚本的绝对路径: D:\python_file\notebook\python开发\djangoHelloWord\test.py
… D:\python_file\notebook\python开发
dirname
去掉文件名(或者文件夹),并返回该文件所在的目录
import os
dir_path = os.path.dirname(__file__)
print('脚本的绝对路径:',dir_path)
print('..', os.path.dirname('..'))
脚本的绝对路径: D:/python_file/notebook/python开发/djangoHelloWord
…
getcwd
import os
dir_path = os.path.dirname(__file__)
os.chdir('..')
print(os.getcwd())
D:\python_file\notebook\python开发
split
切换目录和文件名
import os
print(os.path.split('a/b/c'))
(‘a/b’, ‘c’)