最近接触到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相关常见函数的用法举例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #!/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的常用函数的用法演示和举例