美文网首页
Python 基础篇章5-os模块

Python 基础篇章5-os模块

作者: 庄周幻梦 | 来源:发表于2020-09-06 23:21 被阅读0次

os模块

os模块简介

os模块是我们接触python可能最常用的一个内置模块。os作者是认为是operation system.既然是操作系统自然我们所有在windows/unix系列操作系统上的操作都可以通过os模块去替代。
首先, 附上os模块的文档说明.

import os
print(os.__doc__)
"""
OS routines for NT or Posix depending on what system we're on.

This exports:
  - all functions from posix or nt, e.g. unlink, stat, etc.
  - os.path is either posixpath or ntpath
  - os.name is either 'posix' or 'nt'
  - os.curdir is a string representing the current directory (always '.')
  - os.pardir is a string representing the parent directory (always '..')
  - os.sep is the (or a most common) pathname separator ('/' or '\\')
  - os.extsep is the extension separator (always '.')
  - os.altsep is the alternate pathname separator (None or '/')
  - os.pathsep is the component separator used in $PATH etc
  - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
  - os.defpath is the default search path for executables
  - os.devnull is the file path of the null device ('/dev/null', etc.)

Programs that import and use 'os' stand a better chance of being
portable between different platforms.  Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).
"""
  1. os模块操作路径:os.path
    • abspath 返回路径的绝对值.
      • Return the absolute version of a path.
    import os
    os.path.abspath(__file__)
    # c:\Users\Admin\Desktop\简书.py
    
    • dirname 返回一个路径的父级目录
      • Returns the directory component of a pathname
    os.path.dirname(__file__)
    # c:\Users\Admin\Desktop
    
    • basename 返回路径的最后一个组件
      • Returns the final component of a pathname
    os.path.dirname(__file__)
    # 简书.py
    
    • exists 判断文件是否存在
      • Test whether a path exists. Returns False for broken symbolic links
    os.path.exists(__file__)
    # True
    

相关文章

  • Python 基础篇章5-os模块

    os模块 os模块简介 os模块是我们接触python可能最常用的一个内置模块。os作者是认为是operation...

  • python网络爬虫基础模块安装

    python网络爬虫基础模块安装 python的网络爬虫一般需要requests模块,urllib,urllib2...

  • PyQt5学习笔记1 - Windows下安装PyQt5

    Windows下安装PyQt5 安装Python 往期文章 python 基础篇章1-安装[https://www...

  • Python-01基础-05模块

    Python基础-05模块 python中的模块是什么?简而言之,在python中,一个文件(以“.py”为后缀名...

  • Python 3中的多线程

    Python 3的多线程 Python 3的多线程模块threading在旧版_thread模块基础上进行了更高层...

  • Python: Logging模块实例详解

    Python基础文章集合请移步。 Logging 模块 Quick Start 导入模块后直接logging.wa...

  • Python入门

    Python3教程 安装Python 第一个Python程序 Python基础 函数 高级特性 函数式编程 模块 ...

  • Python-01基础-13功能模块

    Python 基础-13 功能模块 Python2 和 Python3 并存 Python3 安装配置 参考链接:...

  • pyhton基础

    Python基础 - 基础 1. 第一句python - 后缀名是可以是任意? - 导入模块时,如果不是.py文件...

  • python模块基础

    封装分类 (1)容器:(对数据的封装) 列表 特性: 有序,可以修改,元素可以重复,按照索引查询 list1...

网友评论

      本文标题:Python 基础篇章5-os模块

      本文链接:https://www.haomeiwen.com/subject/ncxhektx.html