import os
#该文件所在位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py path1 = os.path.dirname(__file__)
print(path1)#获取当前运行脚本的绝对路径 path2 = os.path.dirname(os.path.dirname(__file__)) #
print(path2)#获取当前运行脚本的绝对路径(去掉最后一个路径) path3 = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
print(path3)#获取当前运行脚本的绝对路径(去掉最后2个路径) path4 = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
print(path4)#获取当前运行脚本的绝对路径(去掉最后3个路径) path5 = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
print(path5)#获取当前运行脚本的绝对路径(去掉最后4个路径) path6 = os.__file__ #获取os所在的目录
print(path6)

结果:

C:\Python352\python.exe D:/第1层/第2层/第3层/第4层/第5层/test11.py
D:/第1层/第2层/第3层/第4层/第5层
D:/第1层/第2层/第3层/第4层
D:/第1层/第2层/第3层
D:/第1层/第2层
D:/第1层
C:\Python352\lib\os.py Process finished with exit code 0

一般需要把os.path.dirname()和os.path.abspath()进行结合使用,我们经常会在django的项目配置文件中看到类似的代码:

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

这里假设该脚本文件为test1.py,绝对路径为:/Users/lowman/test1.py

os.path.abspath(__file__) 返回 /Users/lowman/test1.py
os.path.dirname(os.path.abspath(__file__)) 返回 /Users/lowman
os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 返回 /Users

os.path.abspath(__file__)   返回path规范化的绝对路径。

>>> os.path.abspath('test.csv')
'C:\\Python25\\test.csv' >>> os.path.abspath('c:\\test.csv')
'c:\\test.csv' >>> os.path.abspath('../csv\\test.csv')
'C:\\csv\\test.csv'

python os.path模块常用方法详解

os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法。更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.html

1.os.path.abspath(path) 
返回path规范化的绝对路径。 
 
>>> os.path.abspath('test.csv') 
'C:\\Python25\\test.csv' 
 
>>> os.path.abspath('c:\\test.csv') 
'c:\\test.csv' 
 
>>> os.path.abspath('../csv\\test.csv') 
'C:\\csv\\test.csv' 
 
2.os.path.split(path) 
将path分割成目录和文件名二元组返回。 
 
>>> os.path.split('c:\\csv\\test.csv') 
('c:\\csv', 'test.csv') 
>>> os.path.split('c:\\csv\\') 
('c:\\csv', '') 
 
3.os.path.dirname(path) 
返回path的目录。其实就是os.path.split(path)的第一个元素。 
 
>>> os.path.dirname('c:\\csv\test.csv') 
'c:\\' 
>>> os.path.dirname('c:\\csv') 
'c:\\' 
 
4.os.path.basename(path) 
返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素。 
 
>>> os.path.basename('c:\\test.csv') 
'test.csv' 
>>> os.path.basename('c:\\csv') 
'csv' (这里csv被当作文件名处理了) 
>>> os.path.basename('c:\\csv\\') 
'' 
 
5.os.path.commonprefix(list) 
返回list中,所有path共有的最长的路径。 
 
如: 
>>> os.path.commonprefix(['/home/td','/home/td/ff','/home/td/fff']) 
'/home/td' 
 
6.os.path.exists(path) 
如果path存在,返回True;如果path不存在,返回False。 
 
>>> os.path.exists('c:\\') 
True 
>>> os.path.exists('c:\\csv\\test.csv') 
False 
 
7.os.path.isabs(path) 
如果path是绝对路径,返回True。 
 
8.os.path.isfile(path) 
如果path是一个存在的文件,返回True。否则返回False。 
 
>>> os.path.isfile('c:\\boot.ini') 
True 
>>> os.path.isfile('c:\\csv\\test.csv') 
False 
>>> os.path.isfile('c:\\csv\\') 
False 
 
9.os.path.isdir(path) 
如果path是一个存在的目录,则返回True。否则返回False。 
 
>>> os.path.isdir('c:\\') 
True 
>>> os.path.isdir('c:\\csv\\') 
False 
>>> os.path.isdir('c:\\windows\\test.csv') 
False 
 
10.os.path.join(path1[, path2[, ...]]) 
将多个路径组合后返回,第一个绝对路径之前的参数将被忽略。 
 
>>> os.path.join('c:\\', 'csv', 'test.csv') 
'c:\\csv\\test.csv' 
>>> os.path.join('windows\temp', 'c:\\', 'csv', 'test.csv') 
'c:\\csv\\test.csv' 
>>> os.path.join('/home/aa','/home/aa/bb','/home/aa/bb/c') 
'/home/aa/bb/c' 
 
11.os.path.normcase(path) 
在Linux和Mac平台上,该函数会原样返回path,在windows平台上会将路径中所有字符转换为小写,并将所有斜杠转换为饭斜杠。 
 
>>> os.path.normcase('c:/windows\\system32\\') 
'c:\\windows\\system32\\' 
 
12.os.path.normpath(path) 
规范化路径。 
 
>>> os.path.normpath('c://windows\\System32\\../Temp/') 
'c:\\windows\\Temp' 
 
12.os.path.splitdrive(path) 
返回(drivername,fpath)元组 
 
>>> os.path.splitdrive('c:\\windows') 
('c:', '\\windows') 
 
13.os.path.splitext(path) 
分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作 
 
>>> os.path.splitext('c:\\csv\\test.csv') 
('c:\\csv\\test', '.csv') 
 
14.os.path.getsize(path) 
返回path的文件的大小(字节)。 
 
>>> os.path.getsize('c:\\boot.ini') 
299L 
 
15.os.path.getatime(path) 
返回path所指向的文件或者目录的最后存取时间。 
 
16.os.path.getmtime(path) 
返回path所指向的文件或者目录的最后修改时间

python os.path 的使用的更多相关文章

  1. Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数

    Python  os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...

  2. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  3. 【308】Python os.path 模块常用方法

    参考:Python os.path 模块 参考:python3中,os.path模块下常用的用法总结 01   abspath 返回一个目录的绝对路径. 02   basename 返回一个目录的基名 ...

  4. Python——os.path.dirname(__file__) 与 os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  5. os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用

    python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...

  6. python os.path

    os.path 提供了一些处理文件路径的函数. os.path.abspath(path) 返回绝对路径, 在大多数平台上, os.path.abspath(path) == os.path.norm ...

  7. [python] os.path说明

    os.path - Common pathname manipulations操作 This module implements some useful functions on pathnames. ...

  8. python - os.path,路径相关操作

    python处理系统路径的相关操作: # -*- coding: utf-8 -*- import os # 属性 print '__file__: %s' % __file__ # 绝对路径(包含文 ...

  9. python os.path模块--转载

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

随机推荐

  1. 手机端图片预览和缩放js

    转至:http://blog.sina.com.cn/s/blog_c342e3090102vcxu.html 1.手机端的图片选择和预览 <input type="file" ...

  2. [ Laravel 5.5 文档 ] 底层原理 —— 一次 Laravel 请求的生命周期

     Posted on 2018年3月5日 by  学院君 简介 当我们使用现实世界中的任何工具时,如果理解了该工具的工作原理,那么用起来就会得心应手,应用开发也是如此.当你理解了开发工具如何工作,用起 ...

  3. 2018.09.22 atcoder Snuke's Coloring 2(线段树+单调栈)

    传送门 就是给出一个矩形,上面有一些点,让你找出一个周长最大的矩形,满足没有一个点在矩形中. 这个题很有意思. 考虑到答案一定会穿过中线. 于是我们可以把点分到中线两边. 先想想暴力如何解决. 显然就 ...

  4. 2018.08.20 loj#115. 无源汇有上下界可行流(模板)

    传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...

  5. SwapBuffers的等待,虚伪的FPS(转)

    FPS在实时渲染中扮演着一个重要的角色,也许你会去笑一个不懂FPS是什么的游戏新手,但也许,这只是五十步笑一百步罢了.你能读懂SwapBuffers的深情等待吗?——ZwqXin.com frames ...

  6. PBOCIC读芯片卡流程

    https://blog.csdn.net/kxd_ysheng/article/details/21178101?_t=t PBOCIC读芯片卡流程,参考上面的博客,整理了一下PBOCIC卡读流程. ...

  7. hdu 5066 小球碰撞(物理题)

    http://acm.hdu.edu.cn/showproblem.php?pid=5066 中学物理题 #include <cstdio> #include <cstdlib> ...

  8. maven镜像

    使用maven管理项目,下载依赖jar包的时候,经常会下载很慢,但是如果使用镜像的话,速度超级快~~只要在.m2/setting.xml文件中设置镜像就可以啦~ 本文来自https://yq.aliy ...

  9. 菜鸟——使用bootstrap

    方法一: 直接在页面中加入bootstrap的网址,不需要做其他任何改动 <%-- Created by IntelliJ IDEA. User: JC Date: 2017/2/24 Time ...

  10. KbmMW安装

    系统环境及相关软件版本: Windows 7 64位, Delphi XE Version 15.0.3953.35171 , Indy 10.5.7 kbmMW4.90.04 , kbmMemTab ...