大家好,从本周起早起Python将持续更新由小甜同学从初学者的角度学习Python的笔记,其特点就是全文大多由新手易理解的代码与注释及动态演示刚入门的读者千万不要错过!

为了配合Python办公自动化系列文章,本文带来的是偷学Python第二十六天:OS.path模块的详细使用说明,其他内容将在近期更新完毕,本文目录如下


相对路径与绝对路径


所谓绝对路径就是一个完整的路径,例如C:\windows\system32\cmd.exe


相对路径就是从当前路径开始的路径。使用一个.来表示当前目录,两个点..表示当前的父目录。例如当前目录为C:\windows要描述上述的路径只需要.\system32\cmd.exe或者system32\cmd.exe,前者更为严格后者较为简单;如果当前路径为C:\windows\Wed要找到上述路径就可以../system32\cmd.exe


相对路径在编程中更为常用,因为程序媛永远不会知道用户将程序放在哪个盘里面,所有用相对路径就完美的结局了这个问题!


OS.path模块


Python中的os.path模块主要用于获取文件的属性。

path.abspath()


path.abspath()方法返回绝对路径,参数为一个路径,示例代码如下

import os
file = os.path.abspath(__file__)  # __file__ 表示当前文件
print(file) 

path.basename()


os.path.basename(path) 返回文件名,示例代码如下

import os
file = os.path.basename(__file__)
print(file)  # 02path.basename()方法.py

path.dirname()


path.dirname()方法返回返回path的文件夹,示例代码如下

import os
file = os.path.dirname(__file__)
print(file)  # (此处...)01 基础部分/23os.path模块

path.exists()


path.exists()方法把当前路径包含的~~user修改为家目录,示例代码如下

import os
file = os.path.expanduser("~\\甜甜\\1.txt")  # 一个反反斜线是转义字符
print(file)  # C:\Users\admin\甜甜\1.txt

path.expanduser()


path.expanduser()方法把当前路径包含的~或者~user修改为家目录,示例代码如下

import os
file = os.path.expanduser("~\\甜甜\\1.txt")  # 一个反反斜线是转义字符
print(file)  # C:\Users\admin\甜甜\1.txt

path.expandvars()


path.expandvars()方法对路径中出现的$name 或者 ${name}进行系统环境变量路径的取代.,示例代码如下

import os
file = os.path.expandvars("$PATH")
print(file)  # D:\Program...npm

获取文件的访问/修改/创建时间


  • os.path.getatime(path) 返回最近访问时间(浮点型秒数)

  • os.path.getmtime(path)返回最近文件修改时间

  • os.path.getctime(path)返回文件路径创建的时间

import os
import time
access_time = os.path.getatime(__file__)  # 最近访问时间
modify_time = os.path.getmtime(__file__)  # 最近修改时间
create_time = os.path.getctime("02path.basename()方法.py")  # 返回文件的创建时间
print(access_time)  # 1590401407.8338118
print(modify_time)  # 1590401407.7799587
print(create_time)  # 1590398628.070578
# 利用time模块的ctime方法转变为正常时间
print(time.ctime(access_time))  # Mon May 25 18:14:49 2020

path.getsize()


path.getsize()方法返回返回文件的大小,如果文件不存在抛出异常,示例代码如下

import os
file = os.path.getsize(__file__)  # 返回字节数
print(file)  # 125

判断路径的属性


os.path.isabs/.isfile/.isdir/.islink/.ismount(path)判断路径是否为绝对路径/文件/文件夹/链接/挂载点

import os
file1 = os.path.isabs("09判断路径的属性.py")  # False
file2 = os.path.isfile("09判断路径的属性.py")  # True
file3 = os.path.isdir("..\\")  # True
file4 = os.path.islink(__file__)  # False
file5 = os.path.ismount("D:")  # True
print(file1, file2, file3, file4, file5)

path.join()


os.path.join(path1[, path2[, …]])把目录和文件名合成一个路径

import os
file_name = os.path.basename(__file__)  # 获取文件名
file = os.path.join("D:\\", file_name)  # 拼合路径
print(file)  # D:\10path.join方法.py


path.normcase()


os.path.normcase(path) 在不区分大小写的文件系统上, 它把路径转换为小写字母。在Windows上, 它把正斜杠转换为反斜杠

import os
file = os.path.normcase("D:\\file/1.txt")  # 将一个路径转变为适合自己系统的路径
print(file)  # d:\file\1.txt


path.normpath()


os.path.normpath(path) 规范path的字符串形式

import os
file = os.path.normpath("01 基础部分/23os.path模块/12path.normpath()方法.py")
print(file)  # 01 基础部分\23os.path模块\12path.normpath()方法.py

path.realpath()


os.path.realpath(path) 返回path的真实路径

import os
file = os.path.realpath(__file__)
print(file)  # (省略。。。))01 基础部分\23os.path模块\13path.realpath()方法.py

path.relpath()


os.path.relpath(path[, start]) 从start开始计算相对路径

import os
file = os.path.relpath(__file__, "01 基础部分")
print(file)  # ..\14path.relpath()方法.py


分割路径


  • os.path.split(path) 把路径分割成dirname和basename,返回一个元组

  • os.path.splitdrive(path) 一般用在windows下,返回驱动器名和路径组成的元组

  • os.path.splitext(path) 分割路径,返回路径名和文件扩展名的元组

import os
# 返回文件夹与文件的元组
print(os.path.split(__file__))  # ('Y:/.../01 基础部分/23os.path模块', '15分割路径.py')
# 返回驱动器名和路径组成的元组
print(os.path.splitdrive(__file__))  # ('Y:', '.../01 基础部分/23os.path模块/15分割路径.py')
# 返回路径名和扩展的元组
print(os.path.splitext(__file__))  # ('Y:.../01 基础部分/23os.path模块/15分割路径'


©著作权归作者所有:来自51CTO博客作者mb5fe18e32e4691的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 如何找到属于自己高效学习方法?
  2. Pyecharts制作地图的几种方法评析
  3. 词云图的几种制作方法评测,你pick哪款
  4. 便捷搭建 Zookeeper 服务器的方法,好用,收藏~
  5. 自学系列 | 就谈自学方法!
  6. php性能优化的方法介绍
  7. PHP操作Redis数据库常用方法(总结)
  8. php中比较两个数组差异的方法
  9. 关于CentOS6.x/6.5/6.4/6.3/6.2/7.x 64位安装php5.2的方法详解

随机推荐

  1. Android Progrees处理
  2. Android 8.1 系统锁屏显示流程整理
  3. Using C++ Code in Android Application
  4. Android(安卓)6.0运行时权限解决方案
  5. ScrollView中的LinearLayout不能使用andr
  6. Android不错的图片压缩方法
  7. Android 打开关闭闪光灯工具类
  8. android 的C++代码都加 namespace androi
  9. [置顶] 调用Android发短信接口Intent.ACT
  10. Internal error. Please report to https