Python 路径处理(os.path模块)

2023年6月6日11:05:41
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

  • 作者:xupeng1644
  • 原文链接:https://blog.csdn.net/xp178171640/article/details/116310222
    更新时间:2023年6月6日11:05:41 ,共 4077 字。