python获取文件上一级目录:取文件所在目录的上一级目录
os.path.abspath(os.path.join(os.path.dirname('settings.py'),os.path.pardir))
os.path.pardir是父目录,os.path.abspath是绝对路径
举例具体看一下输出:
print os.path.dirname(os.path.abspath("__file__"))
print os.path.pardir
print os.path.join(os.path.dirname("__file__"),os.path.pardir)
print os.path.abspath(os.path.join(os.path.dirname("__file__"),os.path.pardir))
输出结果:
G:\work\python
.. #这是两个点“..”,也就是上级目录的表示方法
..
G:\work
获取文件当前路径: os.path.dirname(os.path.abspath('__file__'))
python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块。
得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()
返回指定目录下的所有文件和目录名:os.listdir()
函数用来删除一个文件:os.remove()
删除多个目录:os.removedirs(r“c:\python”)
检验给出的路径是否是一个文件:os.path.isfile()
检验给出的路径是否是一个目录:os.path.isdir()
判断是否是绝对路径:os.path.isabs()
检验给出的路径是否真地存:os.path.exists()
返回一个路径的目录名和文件名: