dest_file = r'D:\PythonProject\TestPython\test.py' # 实际存在 dest_fake_file = r'X:\PythonProject\TestPython\test.py'# 实际不存在 dest_dir = r'D:\PythonProject\TestPython' # 实际存在 dest_fake_dir = r'X:\PythonProject\TestPython' # 实际不存在 dest_data = [dest_file, dest_fake_file, dest_dir, dest_fake_dir] 

os.path主要对文件、文件夹进行处理,常用的函数可以分为四类:

  • 解析路径
  • 获取路径与文件名以及将路径与文件名合并成带路径文件名
  • 获取绝对路径
  • 获取文件时间相关数据(时间戳)
  • 判断是否是文件、是否是路径、是否存在

1 解析路径[都不会判断路径是否真实存在]

  • split() 将路径分离成路径和文件名
  • splitext() 将路径分离成路径和文件后缀
  • splitdriver() 将路径费力成盘符和文件路径

测试数据

for one_data in dest_data:     print('--------------------------------------------')     print(f'{one_data}')     print('split: {}'.format(os.path.split(one_data)))     print('splitext: {}'.format(os.path.splitext(one_data)))     print('splitdrive: {}'.format(os.path.splitdrive(one_data))) 
-------------------------------------------- D:\PythonProject\TestPython\test.py split: ('D:\\PythonProject\\TestPython', 'test.py') splitext: ('D:\\PythonProject\\TestPython\\test', '.py') splitdrive: ('D:', '\\PythonProject\\TestPython\\test.py') -------------------------------------------- X:\PythonProject\TestPython\test.py split: ('X:\\PythonProject\\TestPython', 'test.py') splitext: ('X:\\PythonProject\\TestPython\\test', '.py') splitdrive: ('X:', '\\PythonProject\\TestPython\\test.py') -------------------------------------------- D:\PythonProject\TestPython split: ('D:\\PythonProject', 'TestPython') splitext: ('D:\\PythonProject\\TestPython', '') splitdrive: ('D:', '\\PythonProject\\TestPython') -------------------------------------------- X:\PythonProject\TestPython split: ('X:\\PythonProject', 'TestPython') splitext: ('X:\\PythonProject\\TestPython', '') splitdrive: ('X:', '\\PythonProject\\TestPython') 

2 获取路径与文件名以及将路径与文件名合并成带路径文件名[都不会判断路径是否真实存在]

  • basename() 获取文件名
  • dirname() 获取路径
  • join() 将路径和文件名合并成带路径文件名
dest_data = [dest_file, dest_fake_file] for one_data in dest_data:         print('--------------------------------------------')         print(f'{one_data}')         dir_name = os.path.dirname(one_data)         base_name = os.path.basename(one_data)         print('dir_name: {}'.format(dir_name))         print('base_name: {}'.format(base_name))         print('after join: {}'.format(os.path.join(dir_name, base_name))) 
-------------------------------------------- D:\PythonProject\TestPython\test.py dir_name: D:\PythonProject\TestPython base_name: test.py after join: D:\PythonProject\TestPython\test.py -------------------------------------------- X:\PythonProject\TestPython\test.py dir_name: X:\PythonProject\TestPython base_name: test.py after join: X:\PythonProject\TestPython\test.py 

3 获取绝对路径
absdir() 根据相对路径获取绝对路径

import os.path  print(os.path.abspath('.')) # 当前路径为D:\PythonProject\TestPython 
D:\PythonProject\TestPython 

4 获取文件时间相关数据(时间戳)[都会判断路径是否真实存在]

  • getctime() 获取文件创建时间(create)
  • getatime() 获取文件访问时间(access)
  • getmtime() 获取文件修改时间(modify)
dest_data = [dest_file, dest_dir] # 如果加入dest_fake_file或者dest_fake_dir会报错找不到 for one_data in dest_data:     print('--------------------------------------------')     print(f'{one_data}')     print('create time: {}'.format(os.path.getctime(one_data)))     print('access time: {}'.format(os.path.getatime(one_data)))     print('modify time: {}'.format(os.path.getmtime(one_data))) 
-------------------------------------------- D:\PythonProject\TestPython\test.py create time: 1619795451.78109 access time: 1619795451.78109 modify time: 1619795451.78109 -------------------------------------------- D:\PythonProject\TestPython create time: 1619795443.6216414 access time: 1620021754.546589 modify time: 1619795457.3491347 

5 判断是否是文件、是否是路径、是否存在[都会判断路径是否真实存在]

  • exsits() # 文件或目录实际是否存在
  • isfile() # 是否是文件 前提是exisits()为True
  • isdir() # 是否是目录 前提是exisits()为True
for one_data in dest_data:     print('--------------------------------------------')     print(f'{one_data}')     print('is file: {}'.format(os.path.isfile(one_data)))     print('is dir: {}'.format(os.path.isdir(one_data)))     print('is exsit:{}'.format(os.path.exists(one_data))) 
-------------------------------------------- D:\PythonProject\TestPython\test.py is file: True is dir: False is exsit:True -------------------------------------------- X:\PythonProject\TestPython\test.py is file: False is dir: False is exsit:False -------------------------------------------- D:\PythonProject\TestPython is file: False is dir: True is exsit:True -------------------------------------------- X:\PythonProject\TestPython is file: False is dir: False is exsit:False 

热门文章

1月31日更新22.4M/S,2025年最新高速V2ray/Clash/Shadowrocket/SSR订阅链接免费节点地址分享

这一次的节点更新覆盖了香港、美国、新加坡、加拿大、韩国、日本、欧洲等地区,最高速度可达22.4 M/S。只需复制下方的Clash/v2ray订阅链接,在客户端添加后即可正常使用。

2月4日更新18.9M/S,2025年最新高速Clash/Shadowrocket/SSR/V2ray订阅链接免费节点地址分享

这一次的节点更新覆盖了加拿大、新加坡、韩国、美国、香港、欧洲、日本等地区,最高速度可达18.9 M/S。只需复制下方的Clash/v2ray订阅链接,在客户端添加后即可正常使用。

2月13日更新18.3M/S,2025年最新高速V2ray/Shadowrocket/Clash/SSR订阅链接免费节点地址分享

这一次的节点更新覆盖了欧洲、日本、新加坡、加拿大、韩国、美国、香港等地区,最高速度可达18.3 M/S。只需复制下方的Clash/v2ray订阅链接,在客户端添加后即可正常使用。

1月13日更新22.2M/S,2025年最新高速V2ray/SSR/Shadowrocket/Clash订阅链接免费节点地址分享

这一次的节点更新覆盖了日本、香港、欧洲、韩国、加拿大、美国、新加坡等地区,最高速度可达22.2 M/S。只需复制下方的Clash/v2ray订阅链接,在客户端添加后即可正常使用。

领养宠物在哪里领养(领养宠物狗的地方在哪里)

摘要: 本篇文章给大家谈谈领养宠物在哪里领养,以及领养宠物狗的地方在哪里对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。本文目录一览:1、哪里有领养狗狗的?2、...

1月16日更新22.3M/S,2025年最新高速SSR/Clash/V2ray/Shadowrocket订阅链接免费节点地址分享

这一次的节点更新覆盖了欧洲、新加坡、香港、美国、加拿大、日本、韩国等地区,最高速度可达22.3 M/S。只需复制下方的Clash/v2ray订阅链接,在客户端添加后即可正常使用。

动物医院实验室常见设备仪器有哪些(动物医院实验室常见设备仪器有哪些品牌)

摘要: 本篇文章给大家谈谈动物医院实验室常见设备仪器有哪些,以及动物医院实验室常见设备仪器有哪些品牌对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。本文目录一览:1、常用生物医学..

宠物狗领养免费领养宠物的网站叫什么来着(宠物狗领养平台)

摘要: 本篇文章给大家谈谈宠物狗领养免费领养宠物的网站叫什么来着,以及宠物狗领养平台对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。本文目录一览:1、领养宠物的平台有哪些?...

git下载、安装、配置与简单应用

目录 1 git下载、安装与配置 1.1下载与安装 1.1.1登录git官网:Git (git-scm.com)点击击图中红线圈出的Downloads,进入下载页面。 1.1.2根据自己系统,下载相应

58同城宠物狗狗领养(58同城领养小狗狗)

摘要: 今天给各位分享58同城宠物狗狗领养的知识,其中也会对58同城领养小狗狗进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!本文目录一览:1、58上领养狗狗是真的.

归纳