Python - 模块 os

os 模块

  1. 利用os 模块可以使用操作系统的相关功能。
  2. 官方文档地址

os.path 子模块

  1. os.path 子模块提供了与路径相关的操作功能
  2. 官方文档地址
方法解释
os.path.abspath(path)返回绝对路径
os.path.basename(path)Return the base name of pathname path.
os.path.commonpath(paths)Return the longest common sub-path of each pathname in the sequence paths.
os.path.commonprefix(list)Return the longest path prefix (taken character-by-character) that is a prefix of all paths in list.
os.path.dirname(path)返回父文件夹的路径
os.path.exists(path)判断路径是否存在
os.path.lexists(path)Return True if path refers to an existing path.
os.path.expanduser(path)On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user’s home directory.
os.path.expandvars(path)Return the argument with environment variables expanded.
os.path.getatime(path)Return the time of last access of path.
os.path.getmtime(path)Return the time of last modification of path.
os.path.getctime(path)Return the system’s ctime which, on some systems (like Unix) is the time of the last metadata change, and, on others (like Windows), is the creation time for path.
os.path.getsize(path)Return the size, in bytes, of path.
os.path.isabs(path)Return True if path is an absolute pathname.
os.path.isfile(path)Return True if path is an existing regular file.
os.path.isdir(path)Return True if path is an existing directory.
os.path.islink(path)Return True if path refers to an existing directory entry that is a symbolic link.
os.path.ismount(path)Return True if pathname path is a mount point: a point in a file system where a different file system has been mounted.
os.path.join(path, *paths)Join one or more path components intelligently.
os.path.normcase(path)Normalize the case of a pathname.
os.path.normpath(path)Normalize a pathname by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/…/B all become A/B.
os.path.realpath(path)Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system).
os.path.relpath(path, start=os.curdir)Return a relative filepath to path either from the current directory or from an optional start directory.
os.path.samefile(path1, path2)Return True if both pathname arguments refer to the same file or directory.
os.path.sameopenfile(fp1, fp2)Return True if the file descriptors fp1 and fp2 refer to the same file.
os.path.samestat(stat1, stat2)Return True if the stat tuples stat1 and stat2 refer to the same file.
os.path.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.
os.path.splitdrive(path)Split the pathname path into a pair (drive, tail) where drive is either a mount point or the empty string.
os.path.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.
os.path.supports_unicode_filenamesTrue if arbitrary Unicode strings can be used as file names (within limitations imposed by the file system).
>>> # 返回绝对路径
>>> os.path.abspath("./freetuream") 
"Users/freetuream"
>>> # 返回父文件夹路径
>>> os.path.dirname("./freetuream/sth")
"./freetuream"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值