python(第四版阅读心得)(系统工具)(一)

本章将会讲解python常用系统工具的介绍

python中大多数系统级接口都集中在两个模块: sys 和 os

但仍有部分其他标准模块也属于这个领域

如:

常见:

glob   用于文件名扩展

socket  用于网络连接和进程间通信

threading, _thread,queue  用于运行和同步化并发线程

time,timeit 用于获取系统时间相关细节

subprocess,multiprocessing 用于启动和控制并行进程

signal,select,shutil,tempfile 用于多种系统相关任务

不常见:

pySerial  一种串行端口接口

Pexpect 一个用于控制程序建对话的类Except系统

内建函数:

open 文件系统的接口等

但大体而言, sys模块和os模块 是系统工具的核心模块

sys模块主要负责与python解释器相关的组件, 而os模块包含于python所在底层操作系统相对应的变量和函数

随时可以查看sys模块和os模块所能导出的东西:

import sys

print(dir(sys))

[‘__displayhook__‘, ‘__doc__‘, ‘__excepthook__‘, ‘__interactivehook__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘__stderr__‘, ‘__stdin__‘, ‘__stdout__‘, ‘_clear_type_cache‘, ‘_current_frames‘, ‘_debugmallocstats‘, ‘_getframe‘, ‘_git‘, ‘_home‘, ‘_xoptions‘, ‘api_version‘, ‘argv‘, ‘base_exec_prefix‘, ‘base_prefix‘, ‘builtin_module_names‘, ‘byteorder‘, ‘call_tracing‘, ‘callstats‘, ‘copyright‘, ‘displayhook‘, ‘dllhandle‘, ‘dont_write_bytecode‘, ‘exc_info‘, ‘excepthook‘, ‘exec_prefix‘, ‘executable‘, ‘exit‘, ‘flags‘, ‘float_info‘, ‘float_repr_style‘, ‘get_coroutine_wrapper‘, ‘getallocatedblocks‘, ‘getcheckinterval‘, ‘getdefaultencoding‘, ‘getfilesystemencoding‘, ‘getprofile‘, ‘getrecursionlimit‘, ‘getrefcount‘, ‘getsizeof‘, ‘getswitchinterval‘, ‘gettrace‘, ‘getwindowsversion‘, ‘hash_info‘, ‘hexversion‘, ‘implementation‘, ‘int_info‘, ‘intern‘, ‘is_finalizing‘, ‘maxsize‘, ‘maxunicode‘, ‘meta_path‘, ‘modules‘, ‘path‘, ‘path_hooks‘, ‘path_importer_cache‘, ‘platform‘, ‘prefix‘, ‘set_coroutine_wrapper‘, ‘setcheckinterval‘, ‘setprofile‘, ‘setrecursionlimit‘, ‘setswitchinterval‘, ‘settrace‘, ‘stderr‘, ‘stdin‘, ‘stdout‘, ‘thread_info‘, ‘version‘, ‘version_info‘, ‘warnoptions‘, ‘winver‘]

import os

print(dir(os))

[‘F_OK‘, ‘MutableMapping‘, ‘O_APPEND‘, ‘O_BINARY‘, ‘O_CREAT‘, ‘O_EXCL‘, ‘O_NOINHERIT‘, ‘O_RANDOM‘, ‘O_RDONLY‘, ‘O_RDWR‘, ‘O_SEQUENTIAL‘, ‘O_SHORT_LIVED‘, ‘O_TEMPORARY‘, ‘O_TEXT‘, ‘O_TRUNC‘, ‘O_WRONLY‘, ‘P_DETACH‘, ‘P_NOWAIT‘, ‘P_NOWAITO‘, ‘P_OVERLAY‘, ‘P_WAIT‘, ‘R_OK‘, ‘SEEK_CUR‘, ‘SEEK_END‘, ‘SEEK_SET‘, ‘TMP_MAX‘, ‘W_OK‘, ‘X_OK‘, ‘_DummyDirEntry‘, ‘_Environ‘, ‘__all__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘_dummy_scandir‘, ‘_execvpe‘, ‘_exists‘, ‘_exit‘, ‘_get_exports_list‘, ‘_putenv‘, ‘_unsetenv‘, ‘_wrap_close‘, ‘abort‘, ‘access‘, ‘altsep‘, ‘chdir‘, ‘chmod‘, ‘close‘, ‘closerange‘, ‘cpu_count‘, ‘curdir‘, ‘defpath‘, ‘device_encoding‘, ‘devnull‘, ‘dup‘, ‘dup2‘, ‘environ‘, ‘errno‘, ‘error‘, ‘execl‘, ‘execle‘, ‘execlp‘, ‘execlpe‘, ‘execv‘, ‘execve‘, ‘execvp‘, ‘execvpe‘, ‘extsep‘, ‘fdopen‘, ‘fsdecode‘, ‘fsencode‘, ‘fstat‘, ‘fsync‘, ‘ftruncate‘, ‘get_exec_path‘, ‘get_handle_inheritable‘, ‘get_inheritable‘, ‘get_terminal_size‘, ‘getcwd‘, ‘getcwdb‘, ‘getenv‘, ‘getlogin‘, ‘getpid‘, ‘getppid‘, ‘isatty‘, ‘kill‘, ‘linesep‘, ‘link‘, ‘listdir‘, ‘lseek‘, ‘lstat‘, ‘makedirs‘, ‘mkdir‘, ‘name‘, ‘open‘, ‘pardir‘, ‘path‘, ‘pathsep‘, ‘pipe‘, ‘popen‘, ‘putenv‘, ‘read‘, ‘readlink‘, ‘remove‘, ‘removedirs‘, ‘rename‘, ‘renames‘, ‘replace‘, ‘rmdir‘, ‘scandir‘, ‘sep‘, ‘set_handle_inheritable‘, ‘set_inheritable‘, ‘spawnl‘, ‘spawnle‘, ‘spawnv‘, ‘spawnve‘, ‘st‘, ‘startfile‘, ‘stat‘, ‘stat_float_times‘, ‘stat_result‘, ‘statvfs_result‘, ‘strerror‘, ‘supports_bytes_environ‘, ‘supports_dir_fd‘, ‘supports_effective_ids‘, ‘supports_fd‘, ‘supports_follow_symlinks‘, ‘symlink‘, ‘sys‘, ‘system‘, ‘terminal_size‘, ‘times‘, ‘times_result‘, ‘truncate‘, ‘umask‘, ‘uname_result‘, ‘unlink‘, ‘urandom‘, ‘utime‘, ‘waitpid‘, ‘walk‘, ‘write‘]

显示文档字符串:

import sys

print(sys.__doc__)

This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.

Dynamic objects:

argv -- command line arguments; argv[0] is the script pathname if known
path -- module search path; path[0] is the script directory, else ‘‘
modules -- dictionary of loaded modules

displayhook -- called to show results in an interactive session
excepthook -- called to handle any uncaught exception other than SystemExit
To customize printing in an interactive session or to install a custom
top-level exception handler, assign other functions to replace these.

stdin -- standard input file object; used by input()
stdout -- standard output file object; used by print()
stderr -- standard error object; used for error messages
By assigning other file objects (or objects that behave like files)
to these, it is possible to redirect all of the interpreter‘s I/O.

last_type -- type of last uncaught exception
last_value -- value of last uncaught exception
last_traceback -- traceback of last uncaught exception
These three are only available in an interactive session after a
traceback has been printed.

Static objects:

builtin_module_names -- tuple of module names built into this interpreter
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
dllhandle -- [Windows only] integer handle of the Python DLL
winver -- [Windows only] version number of the Python DLL
__stdin__ -- the original stdin; don‘t touch!
__stdout__ -- the original stdout; don‘t touch!
__stderr__ -- the original stderr; don‘t touch!
__displayhook__ -- the original displayhook; don‘t touch!
__excepthook__ -- the original excepthook; don‘t touch!

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
getdlopenflags() -- returns flags to be used for dlopen() calls
getprofile() -- get the global profiling function
getrefcount() -- return the reference count for an object (plus one :-)
getrecursionlimit() -- return the max recursion depth for the interpreter
getsizeof() -- return the size of an object in bytes
gettrace() -- get the global debug tracing function
setcheckinterval() -- control how often the interpreter checks for events
setdlopenflags() -- set the flags to be used for dlopen() calls
setprofile() -- set the global profiling function
setrecursionlimit() -- set the max recursion depth for the interpreter
settrace() -- set the global debug tracing function

或者help(sys) 会解释的更加详细

原文地址:https://www.cnblogs.com/guanjinglin/p/9116062.html

时间: 2024-08-29 07:32:29

python(第四版阅读心得)(系统工具)(一)的相关文章

《python参考手册(第四版)》【PDF】下载

<python参考手册(第四版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382222 内容介绍 本书是权威的Python语言参考指南,内容涉及核心Python语言和Python库的最重要部分.本书内容简洁扼要.可读性强,书中还包括了一些没有在Python官方文档或其他资料中出现过的一些高级的主题. 这一版在内容上进行了全面更新,介绍了Python 2.6和Python 3新引入的编程语言特性和库模块,同时还分析了Python程序

36 Python - 系统编程 系统工具

01系统工具 背景介绍,系统工具就是用于管理系统的工具.如window的命令行工具,linux下的shell脚本,Mac系统叫终端. Python中提供了模块可以直接操作各系统的系统工具,具体模块有很多,主要讲sys和os,其中os支持跨平台,且os.path提供文件及目录工具 原文地址:https://www.cnblogs.com/yijiexi/p/11165406.html

javascript实现移动端网页版阅读器

现在手机上的文本阅读app已经非常丰富,良好的阅读体验与海量的书库常常令我感到无比兴奋. 我想到8年前用一点几寸屏幕的mp3看电子书的情景,顿生一种淡淡的温馨.再久远一些,小的时候,我也经常和小伙伴们组团去书店看白书,也就是白看书.古老的木质书架上那一叠叠厚重的黄皮小说书,在年幼的我们眼里仿佛是比盘子里的午餐肉更加美味可口的东西. 而在当今这个信息化的时代,看书变得空前的便利,可是儿时那种期待和兴奋的感受却消失在了时间的长河. 岁月在流逝,时代在进步. 愿放下所有的浮躁,在新的时代愉快地生活,无

C++ Primer 第四版课后练习解答 习题1.1

注意:本随笔是在<C++Primer(第四版)习题解答(完整版)>中直接抄录的.此处主要是便于本人以后反复阅读. 习题1.1 查看所用的编译器文档,了解它所用的文件命名规范.编译并运行本节的main程序. [解答] 一般而言,C++编译器要求编译的程序保存在文件中.C++程序一般涉及两类文件:头文件和源文件.大多数系统中,文件的名字由文件名和文件后缀(又称扩展名)组成.文件后缀通常表明文件的类型,如头文件的后缀可以是.h或.hpp等:源文件和后缀可以是.cc或.cpp等,具体的后缀与使用的编译

C++ Primer 第四版课后练习解答 习题1.2

注意:本随笔是在<C++Primer(第四版)习题解答(完整版)>中直接抄录的.此处主要是便于本人以后反复阅读. 习题1.2 修改程序使其返回-1.返回值-1通常作为程序运行失败的指示器.然而,系统不同,如何(甚至是否)报告main函数运行失败也不同.重新编译并再次运行程序,看看你的系统如何处理main函数的运行失败指示器. [解答]笔者所使用的Windows操作系统并不报告main函数的运行失败,因此,程序返回-1或返回0运行效果上没有什么区别.但是,如果在DOS命令提示符方式下运行程序,然

Python之路,Day20 - 分布式监控系统开发

Python之路,Day20 - 分布式监控系统开发 本节内容 为什么要做监控? 常用监控系统设计讨论 监控系统架构设计 监控表结构设计 为什么要做监控? –熟悉IT监控系统的设计原理 –开发一个简版的类Zabbix监控系统 –掌握自动化开发项目的程序设计思路及架构解藕原则 常用监控系统设计讨论 Zabbix Nagios 监控系统需求讨论 1.可监控常用系统服务.应用.网络设备等 2.一台主机上可监控多个不同服务.不同服务的监控间隔可不同 3.同一个服务在不同主机上的监控间隔.报警阈值可不同

数据库系统概论(第四版)习题解答

数据库系统概论(第四版) 第1章 绪论 1 .试述数据.数据库.数据库系统.数据库管理系统的概念. 答:( l )数据( Data ) :描述事物的符号记录称为数据.数据的种类有数字.文字.图形.图像.声音.正文等.数据与其语义是不可分的.解析在现代计算机系统中数据的概念是广义的.早期的计算机系统主要用于科学计算,处理的数据是整数.实数.浮点数等传统数学中的数据.现代计算机能存储和处理的对象十分广泛,表示这些对象的数据也越来越复杂.数据与其语义是不可分的. 500 这个数字可以表示一件物品的价格

计算机组成原理_第四版课后习题答案(完整版)

计算机组成原理_第四版课后习题答案(完整版) ?第一章 1.?比较数字计算机和模拟计算机的特点. 解:模拟计算机的特点:数值由连续量来表示,运算过程是连续的: 数字计算机的特点:数值由数字量(离散量)来表示,运算按位进行. 两者主要区别见P1?表1.1. 2.?数字计算机如何分类?分类的依据是什么? 解:分类: 数字计算机分为专用计算机和通用计算机.通用计算机又分为巨型机.大型机. 中型机.小型机.微型机和单片机六类. 分类依据:专用和通用是根据计算机的效率.速度.价格.运行的经济性和适应性来划

VB.NET版机房收费系统---SqlHelper

SqlHelper,最早接触这个词儿的时候,好像是13年的暑假,那个夏天来的比往年来的稍晚一些,呵呵,sqlhelper,翻译成中文就是数据库助手,帮手.百度百科这样对她进行阐述: SqlHelper是一个基于.NET Framework的数据库操作组件.组件中包含数据库操作方法.SqlHelper用于简化我们重复的去写那些数据库连接(SqlConnection),SqlCommand,SqlDataReader等等.SqlHelper 封装过后通常是只需要给方法传入一些参数如数据库连接字符串,