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. macos安装postman

    安装命令 brew cask install postman brew 是从下载源码解压然后./configure && make install,同时会包含相关依存库.并自动配置 好 ...

  2. 泛型约束where条件的使用(通过类型参数动态反射创建实例)

    定义抽象的人类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. 创建cookie

    cookie的创建using System;using System.Collections.Generic;using System.Linq;using System.Web;using Syst ...

  4. Python 函数装饰器简明教程

    定义类的静态方法时,就使用了装饰器.其实面向对象中的静态方法都是使用了装饰器. @staticmethod def jump(): print(" 3 meters high") ...

  5. iOS沙盒机制介绍

    一.沙盒机制 沙盒的概念:沙盒是每一个iOS应用程序都会自动创建的一个文件系统目录(文件夹),而且沙盒还具有独立.封闭.安全的特点. 沙盒机制 iOS中的沙盒不仅仅是一个文件目录,TA其实更是一种安全 ...

  6. 2018.08.16 洛谷P1437 [HNOI2004]敲砖块(二维dp)

    传送门 看起来普通dp" role="presentation" style="position: relative;">dpdp像是有后效性的 ...

  7. 2018.07.08 POJ 2481 Cows(线段树)

    Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...

  8. 多块盘制作成一个lvm

    1.创建pv[root@autodeploy nfs]# pvcreate /dev/vdd1WARNING: xfs signature detected on /dev/vdd1 at offse ...

  9. Spring bean是如何加载的

    Spring bean是如何加载的 加载bean的主要逻辑 在AbstractBeanFactory中doGetBean对加载bean的不同情况进行拆分处理,并做了部分准备工作 具体如下 获取原始be ...

  10. 201709015工作日记--上下文的理解,ASM

    1.Android上下文理解 Android上下文对象,在Context中封装一个所谓的“语境”,Activity.Service.Application都继承自Context,所以在这三者创建时都会 ...