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(多个路径) ...
随机推荐
- 全屏API
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=2679 二.相关文章以 ...
- sublime Text与python3的中文编码错误解决办法
在 linux服务器上运行代码报错: Python3中遇到UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal no ...
- 挪过来的spring mvc 的入门 介绍
目录 一.前言二.spring mvc 核心类与接口三.spring mvc 核心流程图 四.spring mvc DispatcherServlet说明 五.spring mvc 父子上下文的说明 ...
- 老板说你的UI设计的不高级?你肯定没用这7个技巧...
对于每个网页设计师而言,在设计过程中总会碰到需要作出设计决策的时候.也许你的公司并没有全职设计师,而需求上则要求设计出全新的UI:又或者你正在制作一个你自己的个人项目,而你希望它比 Bootstrap ...
- 还在手工制作APP规范文档?这款设计神器你不容错过
之前写了一些关于APP原型文档的文章:一款APP的交互文档从撰写到交付 这次想写下关于APP设计规范文档的内容,规范文档这个东西,实际上大部分中小型公司没有这方面的需求,也没精力去制作这样一个系统性的 ...
- java bulid path 和 WEB-INF/lib 下jar 包区别
用Java Build Path导入包和把包复制到lib下是有区别的,它俩其实不会冲突,也没有什么关系的, Java Build Path是我们编译需要的包, 导入到lib下是程序运行时需要的包 , ...
- predict_proba 的使用
- 2018.08.27 rollcall(非旋treap)
描述 初始有一个空集,依次插入N个数Ai.有M次询问Bj,表示询问第Bj个数加入集合后的排名为j的数是多少 输入 第一行是两个整数N,M 接下来一行有N个整数,Ai 接下来一行有M个整数Bj,保证数据 ...
- hdu-2955(01背包+逆向思维+审题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:注意p和m[i]是被抓的概率,不能直接用,要转换为逃跑的概率,然后将得到的钱视为背包体积再 ...
- UVaLive 3645 Objective: Berlin (最大流)
题意:有n个城市,m条航班.已知每条航班的起点和终点,还有每条航班的载客量.出发时间.到达时间.并且要求在任何一个城市(起点.终点除外)都至少要有30分钟的中转时间,求起点到终点的最大客流量. 析:把 ...