Python开发--os.path

本文通过示例代码详细介绍了 Python 中 os.path 模块的功能及使用方法,包括路径规范化、获取文件属性等常见操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自http://www.cnblogs.com/hongten/p/hongten_python_os_apth.html

python中,os.path模块在处理路径的时候非常有用

下面是我做的demo

运行效果:

=========================================

代码部分:

=========================================

#python os

import os

def abspath(path):
    '''Return a normalized absolutized version of the pathname path'''
    return os.path.abspath(path)

def dirname(path):
    '''Return the directory name of pathname path'''
    return os.path.dirname(path)

def getatime(path):
    '''Return the time of last access of path'''
    return os.path.getatime(path)

def gettime(path):
    '''Return the time of last modification of path'''
    return os.path.gettime(path)

def getsize(path):
    '''Return the size, in bytes, of path. Raise OSError if the file does not exist or is inaccessible.'''
    return os.path.getsize(path)

def is_file(path):
    '''Return True if path is an existing regular file.
    This follows symbolic links, so both islink() and isfile()
    can be true for the same path.'''
    return os.path.isfile(path)

def is_dir(path):
    '''Return True if path is an existing directory. This follows symbolic links,
    so both islink() and isdir() can be true for the same path.'''
    return os.path.isdir(path)

def is_link(path):
    '''Return True if path refers to a directory entry that
    is a symbolic link. Always False if symbolic links are not supported.'''
    return os.path.islink(path)

def splitext(path):
    '''
    Split the pathname path into a pair (root, ext) such that
    root + ext == path, and ext is empty or begins with a period
    and contains at most one period. Leading periods on the basename
    are ignored; splitext('.cshrc') returns ('.cshrc', '').
    '''
    return os.path.splitext(path)

def splitunc(path):
    '''
    Split the pathname path into a pair (unc, rest) so that unc is
    the UNC mount point (such as r'\\host\mount'), if present,
    and rest the rest of the path (such as r'\path\file.ext').
    For paths containing drive letters, unc will always be the
    empty string.
    '''
    return os.path.splitunc(path)

def split(path):
    '''Split the pathname path into a pair, (head, tail) where tail is the last
    pathname component and head is everything leading up to that'''
    return os.path.split(path)
    
def main():
    path_file = 'C:\\test.html'
    path_dir = 'C:\\Windows\\Branding'
    print(abspath(path_file))
    print(dirname(path_dir))
    print(getatime(path_file))
    print(getsize(path_file))
    print(splitext(path_file))
    print(splitunc(path_file))
    print(split(path_file))


if __name__ == '__main__':
    main()


x@pi:~/WiringPi/WiringPi $ sudo apt install -y python3-rpi.gpio python3-spidev python3-can # 通过 pip 安装最新版 (可选) sudo pip3 install RPi.GPIO spidev python-can Reading package lists... Done Building dependency tree... Done Reading state information... Done python3-spidev is already the newest version (20200602~200721-1+bookworm). The following additional packages will be installed: python3-msgpack python3-packaging rpi.gpio-common Suggested packages: python-can-doc The following packages will be REMOVED: python3-rpi-lgpio The following NEW packages will be installed: python3-can python3-msgpack python3-packaging python3-rpi.gpio rpi.gpio-common 0 upgraded, 5 newly installed, 1 to remove and 159 not upgraded. Need to get 301 kB of archives. After this operation, 1,467 kB of additional disk space will be used. Get:2 http://mirrors.zju.edu.cn/raspbian/raspbian bookworm/main armhf python3-packaging all 23.0-1 [32.5 kB] Get:1 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf python3-msgpack armhf 1.0.3-2+b1 [62.0 kB] Get:3 http://mirrors.zju.edu.cn/raspbian/raspbian bookworm/main armhf python3-can all 4.1.0-1 [179 kB] Get:4 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf rpi.gpio-common armhf 0.7.1~a4-1+b2 [6,808 B] Get:5 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf python3-rpi.gpio armhf 0.7.1~a4-1+b2 [21.2 kB] Fetched 301 kB in 4s (85.8 kB/s) (Reading database ... 137316 files and directories currently installed.) Removing python3-rpi-lgpio (0.6-0~rpt1) ... Selecting previously unselected package python3-msgpack. (Reading database ... 137301 files and directories currently installed.) Preparing to unpack .../python3-msgpack_1.0.3-2+b1_armhf.deb ... Unpacking python3-msgpack (1.0.3-2+b1) ... Selecting previously unselected package python3-packaging. Preparing to unpack .../python3-packaging_23.0-1_all.deb ... Unpacking python3-packaging (23.0-1) ... Selecting previously unselected package python3-can. Preparing to unpack .../python3-can_4.1.0-1_all.deb ... Unpacking python3-can (4.1.0-1) ... Selecting previously unselected package rpi.gpio-common:armhf. Preparing to unpack .../rpi.gpio-common_0.7.1~a4-1+b2_armhf.deb ... Unpacking rpi.gpio-common:armhf (0.7.1~a4-1+b2) ... Selecting previously unselected package python3-rpi.gpio. Preparing to unpack .../python3-rpi.gpio_0.7.1~a4-1+b2_armhf.deb ... Unpacking python3-rpi.gpio (0.7.1~a4-1+b2) ... Setting up rpi.gpio-common:armhf (0.7.1~a4-1+b2) ... Setting up python3-rpi.gpio (0.7.1~a4-1+b2) ... Setting up python3-packaging (23.0-1) ... Setting up python3-msgpack (1.0.3-2+b1) ... Setting up python3-can (4.1.0-1) ... Processing triggers for man-db (2.11.2-2) ... error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. For more information visit http://rptl.io/venv note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification. x@pi:~/WiringPi/WiringPi $
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值