python中os模块简单使用
import os
import sys
# 获取文件所在目录:
print(os.path.realpath(__file__))
------------------------------------------------------------------------------------------------------------
E:\练习文档\python笔记\写入文件\os.py
#os.path.slipt(path)分割成目录和文件
print(os.path.split(os.path.realpath(__file__)))
------------------------------------------------------------------------------------------------------------
('E:\\练习文档\\python笔记\\写入文件', 'os.py')
#dirname(path) 返回 path目录
print(os.path.split((os.path.dirname(__file__))))
------------------------------------------------------------------------------------------------------------
('e:/练习文档/python笔记', '写入文件')
#abspath()获取 当前文件所有路径
print(os.path.abspath(__file__))
------------------------------------------------------------------------------------------------------------
e:\练习文档\python笔记\写入文件\os.py
#sys.path.append(路径)加入环境变量中去
paths = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
print(paths)
------------------------------------------------------------------------------------------------------------
e:\练习文档\python笔记
#将'e:\练习文档\python笔记'路径添加到环境变量中
sys.path.append(paths)