最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【整理】Python中os.path的常用函数的用法演示和举例

Python crifan 1339浏览 0评论

最近接触到Python的os.path的,除了常见函数外的其他一些,比如splitext

所以想要去搞清楚还有其他哪些常见函数。

python sys info

How to get the system info with Python? – Stack Overflow

https://stackoverflow.com/questions/3103178/how-to-get-the-system-info-with-python

28.1. sys — System-specific parameters and functions — Python 2.7.17 documentation

https://docs.python.org/2/library/sys.html

最后整理如下:

【总结】

Python中os.path相关常见函数的用法举例:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Crifan Li
# Update: 20191219
# Function: Demo os.path common used functions

import os

def osPathDemo():
    currentSystemInfo = os.uname()
    print("currentSystemInfo=%s" % (currentSystemInfo, ))
    # currentSystemInfo=posix.uname_result(sysname='Darwin', nodename='xxx', release='18.7.0', version='Darwin Kernel Version 18.7.0: Sat Oct 12 00:02:19 PDT 2019; root:xnu-4903.278.12~1/RELEASE_X86_64', machine='x86_64')

    pathSeparatorInCurrentOS = os.path.sep
    print("pathSeparatorInCurrentOS=%s" % pathSeparatorInCurrentOS)
    # pathSeparatorInCurrentOS=/

    fullFilePath = "/Users/limao/dev/crifan/python/notEnoughUnpack/Snip20191212_113.png"
    print("fullFilePath=%s" % fullFilePath)
    # fullFilePath=/Users/limao/dev/crifan/python/notEnoughUnpack/Snip20191212_113.png

    dirname = os.path.dirname(fullFilePath)
    print("dirname=%s" % dirname)
    # dirname=/Users/limao/dev/crifan/python/notEnoughUnpack
    basename = os.path.basename(fullFilePath)
    print("basename=%s" % basename)
    # basename=Snip20191212_113.png
    joinedFullPath = os.path.join(dirname, basename)
    print("joinedFullPath=%s" % joinedFullPath)
    # joinedFullPath=/Users/limao/dev/crifan/python/notEnoughUnpack/Snip20191212_113.png
    isSame = (fullFilePath == joinedFullPath)
    print("isSame=%s" % isSame)
    # isSame=True

    root, pointSuffix = os.path.splitext(fullFilePath)
    print("root=%s, pointSuffix=%s" % (root, pointSuffix))
    # root=/Users/limao/dev/crifan/python/notEnoughUnpack/Snip20191212_113, pointSuffix=.png
    head, tail = os.path.split(fullFilePath)
    print("head=%s, tail=%s" % (head, tail))
    # head=/Users/limao/dev/crifan/python/notEnoughUnpack, tail=Snip20191212_113.png
    drive, tail = os.path.splitdrive(fullFilePath)
    print("drive=%s, tail=%s" % (drive, tail))
    # drive=, tail=/Users/limao/dev/crifan/python/notEnoughUnpack/Snip20191212_113.png

    curPath = os.getcwd()
    print("curPath=%s" % curPath)
    # curPath=/Users/limao/dev/crifan/python
    relativePath = os.path.relpath(fullFilePath)
    print("relativePath=%s" % relativePath)
    # relativePath=notEnoughUnpack/Snip20191212_113.png

    isFile = os.path.isfile(fullFilePath)
    print("isFile=%s" % isFile)
    # isFile=True
    isDirectory = os.path.isdir(fullFilePath)
    print("isDirectory=%s" % isDirectory)
    # isDirectory=False

    fileSize = os.path.getsize(fullFilePath)
    print("fileSize=%s" % fileSize)
    # fileSize=368810

    isFileOrFolderRealExist = os.path.exists(fullFilePath)
    print("isFileOrFolderRealExist=%s" % isFileOrFolderRealExist)
    # isFileOrFolderRealExist=True

if __name__ == "__main__":
    osPathDemo()

所有内容,直接看代码,即可很清楚。

转载请注明:在路上 » 【整理】Python中os.path的常用函数的用法演示和举例

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
48 queries in 0.120 seconds, using 18.75MB memory