python os.path 的使用
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 的使用的更多相关文章
- 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__)返回 ...
- 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 ...
- 【308】Python os.path 模块常用方法
参考:Python os.path 模块 参考:python3中,os.path模块下常用的用法总结 01 abspath 返回一个目录的绝对路径. 02 basename 返回一个目录的基名 ...
- 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 ...
- os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用
python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...
- python os.path
os.path 提供了一些处理文件路径的函数. os.path.abspath(path) 返回绝对路径, 在大多数平台上, os.path.abspath(path) == os.path.norm ...
- [python] os.path说明
os.path - Common pathname manipulations操作 This module implements some useful functions on pathnames. ...
- python - os.path,路径相关操作
python处理系统路径的相关操作: # -*- coding: utf-8 -*- import os # 属性 print '__file__: %s' % __file__ # 绝对路径(包含文 ...
- python os.path模块--转载
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...
随机推荐
- mysql的头文件
yum install mysql /usr/include/mysql yum install mysql-devel
- Android系统root破解原理分析
http://dengzhangtao.iteye.com/blog/1543494 root破解过程的终极目标是替换掉系统中的su程序.但是要想替换掉系统中su程序本身就是需要root权限的,怎样在 ...
- python数据类型2
一 文件格式补充 在python3中,除字符串外,所有数据类型在内存中的编码格式都是utf-8,而字符串在内存中的格式是Unicode的格式. 由于Unicode的格式无法存入硬盘中,所以这里还有一种 ...
- tomcat中如何配置虚拟路径
第一步:打开server.xml配置文件.在Host节点里写上该行代码: <Context path="/upload" docBase="E:\upload&qu ...
- 2018.10.14 bzoj1915: 奶牛的跳格子游戏(单调队列优化dp)
传送门 NOIP练习题. f[i]f[i]f[i]表示去的时候选了iii且回来的时候第一步走的是i−1i-1i−1的最优值. 显然f[i]=maxf[i]=maxf[i]=max{f[j]−sum[j ...
- hdu-1055(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1055 题意:给一棵树涂色,这棵树的每个节点都有自己的价值Ci,涂色的原则是只由这个节点的父节点涂色之后 ...
- excel绝对引用中间添加符号
=F1&"_"&J1 绝对引用 相对引用 按F4 然后复制全部,选择性黏贴,值和数字即可
- Android draw Rect 坐标图示
前两天在博客发了在例子 android Canvas类介绍 http://byandby.javaeye.com/blog/825330 建议大家 点进去 看一看 不然下边没办法 继续啊. 我还是把这 ...
- Create Your Content and Structure
The original page source Content is the most important aspect of any site. So let's design for the c ...
- 【C++】C++中的虚函数与纯虚函数
C++中的虚函数 先来看一下实际的场景,就很容易明白为什么要引入虚函数的概念.假设我们有一个基类Base,Base中有一个方法eat:有一个派生类Derived从基类继承来,并且覆盖(Override ...