Python3内置模块

2023-11-12

1、os

all functions from posix, nt or ce, e.g. unlink, stat, etc.


os.name is either 'posix', 'nt' or 'ce'.
os.curdir is a string representing the current directory ('.' or ':')
os.pardir is a string representing the parent directory ('..' or '::')
os.sep is the (or a most common) pathname separator ('/' or ':' 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.)

os.environ: 获取操作系统的环境变量值

FUNCTIONS

execl(file, *args)
execle(file, *args)
execlp(file, *args)
execlpe(file, *args)
execvp(file, args)
execvpe(file, args, env)
fdopen(fd, *args, **kwargs)
fsdecode(filename)
fsencode(filename)
popen(cmd, mode='r', buffering=-1) # Supply os.popen()
spawnl(mode, file, *args)
spawnle(mode, file, *args)
spawnlp(mode, file, *args)
spawnlpe(mode, file, *args)
spawnv(mode, file, args)
spawnve(mode, file, args, env)
spawnvp(mode, file, args)
spawnvpe(mode, file, args, env)

chdir(path):改变目录

os.getlogin():取得当前登录用户

os.getpid():取得当前的进程ID;    os.getppid():取得当前的进程的父进程ID

os.system("vi dmi.txt") :执行os命令


get_exec_path(env=None)

   取环境变量里的PATH值:可以带一个环境变量的值(是一个字典)作为参数;不带的话,取系统的环境变量。

getenv(key, default=None)
  取操作系统的某个环境变量值,必须指明一个环境变量名称。注意:linux下大小写敏感。

makedirs(name, mode=511, exist_ok=False)
makedirs(name [, mode=0o777][, exist_ok=False])
  递归建立目录:makedirs("./a/b/c/d")。mkdir只能在当前目录下建立单级的目录,makedirs可以一次建立许多级目录

mkdir(path, mode=0o777, *, dir_fd=None)
  当前目录下建立单级的目录。


putenv(key, value)
  改变或追加环境变量。经过测试,没有写入操作系统中!意外的是,直接修改 os.environ["key"]="value",或将其赋值后修改:s = os.environ; s["key"]="value" 反而可以在 python 里 以 os.getenv("key") 取得刚才设置的值!

removedirs(name)
  “递归删除目录:若有目录 /a/b/c/d/e,发出 removedirs("/a/b/c/d/e"):若e为空,则删除e;此时若d为空,则删除d。依次类推,直至/ (符合条件,直至删除 /a 目录)

renames(old, new)
  文件或目录改名

unsetenv(key)
  删除环境变量:在windows下失败(win7+python3.4);在linux下执行成功但仍然可以用os.getenv("key")取得前面unsetenv("key")的哪个环境变量

walk(top, topdown=True, οnerrοr=None, followlinks=False)
  Directory tree generator.
  生成一个目录树。没一级目录返回一个3元的元组:路径,[目录s],[文件s]
    当前路径只有一个;但是,可能有多个子目录或没有子目录(为空);可能有多个文件或没有文件(为空)。故,目录s 与 文件s 均为列表。
    当前目录的子目录,则在下一个元组里分别列出。

In [101]: fs = os.walk(".\\py")

In [102]: for f in fs:
...: print(f)
...:
('.\\py', ['gtk实例', 'text'], ['fibonacci(生成器).py', 'fibo_dg.py', 'fileop.py', 'gbk.bat', 'getdmi.py', 'getdmiinfo.py', 'getwmiinfo.py','jjb.py'])
('.\\py\\gtk实例', [], ['redemo.py', 'sortvisu.py', 'ss1.py'])
('.\\py\\text', [], ['佳人+no.txt', '佳人.txt', '佳人ansi.txt', '佳人utf8.txt'])

In [103]:

 

2、sys

   import os后,直接以 os.sys.加下面的内容,即可调用sys的相关变量和函数。

静态变量
copyright -- copyright notice pertaining to this interpreter
exec_prefix -- prefix used to find the machine-specific Python library
executable -- absolute path of the executable binary of the Python interpreter
float_info -- a struct sequence with information about the float implementation.
float_repr_style -- string indicating the style of repr() output for floats
hash_info -- a struct sequence with information about the hash algorithm.
hexversion -- version information encoded as a single integer
implementation -- Python implementation information.
int_info -- a struct sequence with information about the int implementation.
maxsize -- the largest supported length of containers.
maxunicode -- the value of the largest Unicode code point
platform -- platform identifier
prefix -- prefix used to find the Python library
thread_info -- a struct sequence with information about the thread implementation.
version -- the version of this interpreter as a string
version_info -- version information as a named tuple

Functions:

displayhook() -- print an object to the screen, and save it in builtins._
excepthook() -- print an exception and its traceback to sys.stderr
exc_info() -- return thread-safe information about the current exception
exit() -- exit the interpreter by raising SystemExit
getallocatedblocks(): 返回当前已分配的内存块数

getdefaultencoding():返回当前的默认字符串编码

getfilesystemencoding():返回操作系统的字符编码:windows==>mbcs,  linux==>utf-8

getrecursionlimit(): 返回 递归的最大层数

getswitchinterval()/setswitchinterval(n): 获取/设置python的线程切换时间。系统默认为0.005s。

setprofile(function):返回 function的执行效率分析。

 有 部分使用C语言实现的cProfile。下面是cProfile的使用方法:

from cProfile import Profile
import math
......定义测试函数比如test
prof = Profile()
prof.runcall(test)
prof.print_stats()

 

 

 

 

转载于:https://www.cnblogs.com/Afisher/p/9391989.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python3内置模块 的相关文章

随机推荐

  • C1认证:作业四

    1 任务背景 为了摆脱 成为 只会CRUD 没有自我核心竞争力 无思想的编程的大龄码农 所以 开展了本次实验 目的是 去了解算法 去欣赏它的美 去感受它的优雅 使自己成为有灵魂的工程师 2 任务大纲 一 任务一 轮播图片 HTML CSS
  • 开源 LLM (大语言模型)整理(一)

    Large Language Model LLM 即大规模语言模型 是一种基于深度学习的自然语言处理模型 它能够学习到自然语言的语法和语义 从而可以生成人类可读的文本 所谓 语言模型 就是只用来处理语言文字 或者符号体系 的 AI 模型 发
  • C++模板类中的成员函数以及模板函数在类外定义的方式

    如下模板类 template
  • 零跑汽车财报解读:短跑“增程”双动力,长跑“自研”加速度

    3月21日晚间 零跑汽车交出了上市后的首份年度成绩单 纵观零跑汽车这份财报 其2022年的发展不乏亮点 2022年 零跑汽车实现营收123 85亿元 同比增长295 41 以远超行业的水平实现营收的迅速增长 充分证明了零跑汽车强大的经营韧性
  • opencv从内存缓冲区中读取图像数据

    1 从内存中加载图像数据 s Image Info stDispImgInfo spImgInfo gt m stDispInfo cv InputArray pic arr stDispImgInfo pImageBuf nWidth n
  • 如何ping一个网段下的所有IP地址,并保存返回结果

    循环的ping一个网段的所有IP地址 for L i in 1 1 255 do ping n 1 w 60 192 168 1 i find 回复 gt gt d pingall txt n 表示一次ping几次 w 表示一次ping60
  • 稳定性和高可用如何保障?一手测评华为云网站高可用解决方案

    一 前言 在如今科技高速发展的时代 几乎每个企业都依赖互联网 离不开互联网 很多企业的业务也都依托于互联网 比如我们熟知的电商 股市 直播 甚至是用于乘坐地铁 公交买票过闸的APP 如今可以说是一个互联网时代的完全体 但你们是否想过一个问题
  • 大神之路-起始篇

    欢迎关注 WeiyiGeek 公众号 设为 星标 每天带你 基础入门 到 进阶实践 再到 放弃学习 涉及 网络安全运维 应用开发 物联网IOT 学习路径 个人感悟 等知识 花开堪折直须折 莫待无花空折枝 文章目录 第 4 部分 计算机软件与
  • k8s学习(四) k8s使用nodeport方式配置service对外暴露服务

    Pod是有生命周期的 使用凡人皆有一死来描述pod很贴切 当一个工作节点 node 销毁时 节点上运行的pods也会被销毁 ReplicationController会动态地在其他节点上创建Pod来保持应用程序的运行 每一个Pod都有一个独
  • IDEA的作用

    IDEA IntelliJ IDEA 是一款由 JetBrains 开发的 Java 集成开发环境 IDE 它不仅支持 Java 开发 还支持多种其他编程语言和框架 包括 Kotlin Scala Groovy Android Spring
  • 【Linux运维】Ubuntu Server的无密码开机自动登录

    需求 最近我们在开发一个基于树莓派的小盒子 我们会采购一些树莓派 装好外盒 装好arm版本的 Ubuntu Server 20 04 系统 并且装上我们开发的配套软件 对接我们云端的服务 最终把小盒子卖给用户并提供一些收费的服务 我们在安装
  • ORACLE如何实现行转列

    可通过正则截取实现 例如下图 将 1 2 字符串通过 截取 分为两行数据 Select Regexp Substr 1 2 1 Level As SKU From Dual Connect By Regexp Substr 1 2 1 Le
  • 2022年安全员-C证操作证考试题库及在线模拟考试

    题库来源 安全生产模拟考试一点通公众号小程序 2022安全员 C证理论题库系安全员 C证全部考试题库上机考试练习题 2022年安全员 C证操作证考试题库及在线模拟考试根据安全员 C证最新教材汇编 安全员 C证考试模拟题随时根据安全生产模拟考
  • 一、robotframework 安装

    测试环境 windows 安装前提 已安装python robotframework基于python环境 python安装过程略过 安装的软件参考的这篇博文robot framework自动化测试参考手册 安装python库 安装robot
  • Linux下静态库生成和使用

    一 静态库概念 1 库是预编译的目标文件 object files 的集合 它们可以被链接进程序 静态库以后缀为 a 的特殊的存档 archive file 存储 2 标准系统库可在目录 usr lib与 lib中找到 比如 在类Unix系
  • layui原生框架下,展示、替换图片(修改页面)

    最终页面效果如下 由于图片没有资源路径 所以没有展示出来 图片展示可以忽略 代码如下 div class layui form item div
  • JDK的安装及配置详细图文教程(win10)

    JDK的下载 进入官网 选择Products下的Software下的java 官网 下拉 找到java SE页面并选择Oracle JDK 进入后选择JDK Download 然后就会进入到jdk最新版本的下载界面 选择系统对应的下载安装包
  • Linux创建用户并修改shell类型

    base root 57beff3260ef sudo su test exit base root 57beff3260ef apt get install zsh base root 57beff3260ef echo SHELL bi
  • react html 显示,react如何控制元素的显示与隐藏功能?

    react如何控制元素显示与隐藏 在vue中常用v if和v show指令 react中用什么方法呢 下面本篇文章给大家介绍一下 有一定的参考价值 有需要的朋友可以参考一下 希望对大家有所帮助 下面说我知道的三种方法 1 通过 state
  • Python3内置模块

    1 os all functions from posix nt or ce e g unlink stat etc os name is either posix nt or ce os curdir is a string repres