Python教程系列OS模块的使用
python代码详细如下:
import os
#获取当前工作目录
b=os.getcwd()
print(b)
#获取绝对路径 这个方法重要
c=os.path.abspath(os.getcwd())
print(c)
#对路径进行拼接操作
d=os.path.join(os.path.abspath(os.getcwd()),'haha.py')
print(d)
#判断文件是否存在
e=os.path.exists(os.path.abspath(os.getcwd()))
print(e)
#列取当前目录下的所有文件目录
f = os.listdir(os.path.abspath(os.getcwd()))
print(f)
python中os代码结果如下:
注意:
使用前需导入os模块 import os
os.path.join(os.path.abspath(os.getcwd())获取绝对路径