python中的os.path模块
来源:互联网 发布:相册网站设计源码 编辑:程序博客网 时间:2024/05/20 10:21
1,python os.path模块
os.path.abspath(path) #返回绝对路径
os.path.basename(path) #返回文件名
os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。
os.path.dirname(path) #返回文件路径
os.path.exists(path) #路径存在则返回True,路径损坏返回False
os.path.lexists #路径存在则返回True,路径损坏也返回True
os.path.expanduser(path) #把path中包含的"~"和"~user"转换成用户目录
os.path.expandvars(path) #根据环境变量的值替换path中包含的”$name”和”${name}”
os.path.getatime(path) #返回最后一次进入此path的时间。
os.path.getmtime(path) #返回在此path下最后一次修改的时间。
os.path.getctime(path) #返回path的大小
os.path.getsize(path) #返回文件大小,如果文件不存在就返回错误
os.path.isabs(path) #判断是否为绝对路径
os.path.isfile(path) #判断路径是否为文件
os.path.isdir(path) #判断路径是否为目录
os.path.islink(path) #判断路径是否为链接
os.path.ismount(path) #判断路径是否为挂载点()
os.path.join(path1[, path2[, ...]]) #把目录和文件名合成一个路径
os.path.normcase(path) #转换path的大小写和斜杠
os.path.normpath(path) #规范path字符串形式
os.path.realpath(path) #返回path的真实路径
os.path.relpath(path[, start]) #从start开始计算相对路径
os.path.samefile(path1, path2) #判断目录或文件是否相同
os.path.sameopenfile(fp1, fp2) #判断fp1和fp2是否指向同一文件
os.path.samestat(stat1, stat2) #判断stat tuple stat1和stat2是否指向同一个文件
os.path.split(path) #把路径分割成dirname和basename,返回一个元组
os.path.splitdrive(path) #一般用在windows下,返回驱动器名和路径组成的元组
os.path.splitext(path) #分割路径,返回路径名和文件扩展名的元组
os.path.splitunc(path) #把路径分割为加载点与文件
os.path.walk(path, visit, arg) #遍历path,进入每个目录都调用visit函数,visit函数必须有
3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有
文件名,args则为walk的第三个参数
os.path.supports_unicode_filenames #设置是否支持unicode路径名2. 正确的方法
比如C:\test目录下还有一个名为sub的目录;C:\test目录下有getpath.py,sub目录下有sub_path.py,getpath.py调用sub_path.py;我们在C:\test下执行getpath.py。如果我们在sub_path.py里面使用sys.path[0],那么其实得到的是getpath.py所在的目录路径“C:\test”,因为Python虚拟机是从getpath.py开始执行的。如果想得到sub_path.py的路径,那么得这样:
os.path.split(os.path.realpath(__file__))[0]
[Get Code]
其中__file__虽然是所在.py文件的完整路径,但是这个变量有时候返回相对路径,有时候返回绝对路径,因此还要用os.path.realpath()函数来处理一下。也即在这个例子里,os.path.realpath(__file__)输出是“C:\test\sub\sub_path.py”,而os.path.split(os.path.realpath(__file__))[0]输出才是“C:\test\sub”。
0 0
- python中的os.path模块
- Python 中的 os.path 模块说明
- Python os.path模块
- Python os.path模块
- [python] os.path模块
- python os.path模块
- Python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- Python os.path模块
- Python os.path模块
- c 差缺补漏
- runtime运行时
- maven资料合集
- Spring Data Redis
- fopen参数汇总
- python中的os.path模块
- 获取文件编码chardet
- linux awk 内置函数详细介绍(实例)
- 从 MVC 到前后端分离
- 密度聚类DBSCAN原理及代码实现
- Binary Tree Preorder Traversal
- js 计算两个日期之间的天数
- 生成fftw3的lib文件
- Vijos P1485 传球游戏(动态规划)