偷学Python第二十六天:PythonOS.path模块的详细使用说明

人生苦短我用Python

偷学Python第二十六天:PythonOS.path模块的详细使用说明

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。——苏轼

今日学习目标

Python中的OS模块

今日学习内容

相对路径和绝对路径

所谓绝对路径就是一个完整的路径,例如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)  # (此处省略几个字))\01 基础部分\23os.path模块\01os.path.abspath.py

__file__ 表示当前文件

path.basename()方法

os.path.basename(path) 返回文件名

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

os.path.dirname(path)返回path的文件夹

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

os.path.exists(path)判断一个路径是否存在,存在返回True,否则返回False

import os
file = os.path.exists(__file__)
file1 = os.path.exists("tt.txt")
print(file)  # True
print(file1)  # False
path.expanduser()方法

os.path.expanduser(path)把当前路径包含的~或者~user修改为家目录

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

os.path.expandvars(path)对路径中出现的$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()方法

os.path.getsize(path)返回文件的大小,如果文件不存在抛出异常

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分割路径', '.py')

今日学习总结

学习了相对路径和绝对路径的概念,学习了os.path的各种方法

明日学习计划

Python的OS模块

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一碗周.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值