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. sublime的坑

  2. windows驱动

    DriveEntry() 启动 停止 接口函数 驱动程序名 驱动程序版本号 异常处理 是否运行 声明LPOReceive类型的函数 声明函数指针(外部传进来的回调函数) 存放配置字符串 本机IP 串口 ...

  3. Zookeeper 系列(二)安装配制

    Zookeeper 系列(二)安装配制 一.Zookeeper 的搭建方式 Zookeeper 安装方式有三种,单机模式和集群模式以及伪集群模式. 单机模式 :Zookeeper 只运行在一台服务器上 ...

  4. 函数数组demo

    #include <stdio.h> #include <string.h> typedef int(*service_func)(char *,char *); struct ...

  5. unidac 6.0.1 与kbmmw 的一点小摩擦

    unidac 6.0.1  出来了,虽然支持sql server 直连等新特性,但是由于内部改动比较大, 导致与kmmmw 的集成起来存在有点小问题,就是如果数据库不是interbase 或者fire ...

  6. 2018.10.14 loj#6003. 「网络流 24 题」魔术球(最大流)

    传送门 网络流好题. 这道题可以动态建图. 不难想到把每个球iii都拆点成i1i_1i1​和i2i_2i2​,每次连边(s,i1),(i2,t)(s,i_1),(i_2,t)(s,i1​),(i2​, ...

  7. UVa 11039 Building designing (贪心+排序+模拟)

    题意:给定n个非0绝对值不相同的数,让他们排成一列,符号交替但绝对值递增,求最长的序列长度. 析:我个去简单啊,也就是个水题.首先先把他们的绝对值按递增的顺序排序,然后呢,挨着扫一遍,只有符号不同才计 ...

  8. MODIS产品分析和数据处理

    ENVI+IDL 17种MODIS产品的功能解释 https://wenku.baidu.com/view/6fd329dcf524ccbff0218440.html ENVI读取MODIS数据大致步 ...

  9. (线段树) Count the Colors --ZOJ --1610

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F http://acm.zju.edu.cn/onli ...

  10. ESP32应用程序的内存布局

    应用程序内存布局 ESP32芯片具有灵活的内存映射功能.本节介绍ESP-IDF在默认情况下如何使用这些功能. ESP-IDF中的应用程序代码可以放置在以下内存区域之一中. IRAM(指令RAM) ES ...