This blog introduces the standard libraries in common, beloved Python.
SYS
It can be literally seen as the abbreviation for the system. This module provides access to variables and functions that are closely related to the Python parser.
argv 命令行参数exit 退出当前程序modules 映射模块名到载入模块的字典path 目录platform 平台标识符stdin 标准输入stdout 标准输出stderr 标准错误流
Apply, reverse Print command name parameter:
import sysargs = sys.argv[1:]args.reverse()print ‘ ‘.join(args)
OS
OS is the operating system.
The following are more important:
environ 对环境变量进行映射system 在子shell中执行操作系统命令sep 路径中的分隔符pathsep 分隔路径的分隔符linesep 行分隔符urandom 返回n字节的加密强随机数据
Fileinput
Working with files
input 便于遍历多个输入流中的行filename() 返回当前文件的名称lineno() 返回当前累计的行数filelineno() 返回当前文件的行数isfirstline()检查当前行是否是文件中的第一行isstdin() 检查最后一行是否来自sys.stdinnextfile() 关闭当前文件,移动到下一个文件close() 关闭序列
App, add line numbers for Python scripts:
impotr fileninputfor line in fileninput.input(inplace = True): line = line.rstrip() num = fileinput.lineno() print ‘%-40s # %2i ‘ % (line, num)
Python basics-People's Favorite standard library (sys OS fileinput)