常见内置模块
什么叫做模块?
Import xxx xxx就是模块 .py文件也是模块
模块的分类:
内置模块
第三方模块 需要安装
在线安装
或者:
离线安装 下载好所需要的安装包zip------解压安装包setup.py----
—cmd------python install setup.py
模块导入的问题:
Import 模块名
Import 模块名 as 别名
from 包 import 模块的名称
random模块
用来产生随机数(伪随机数)
randint()-------产生随机整数[m,n]
random()----------产生0-1的随机数[0,1)
uniform()--------产生正态分布的随机数
randrange()---------产生一个范围之内的随机数
choices()---------用在序列里的(容器)(有序的序列)随机筛取一个元素
无序会报错:
math模块
用于数学运算
[‘doc’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘acos’, ‘acosh’, ‘asin’, ‘asinh’, ‘atan’, ‘atan2’, ‘atanh’, ‘ceil’, ‘comb’, ‘copysign’, ‘cos’, ‘cosh’, ‘degrees’, ‘dist’, ‘e’, ‘erf’, ‘erfc’, ‘exp’, ‘expm1’, ‘fabs’, ‘factorial’, ‘floor’, ‘fmod’, ‘frexp’, ‘fsum’, ‘gamma’, ‘gcd’, ‘hypot’, ‘inf’, ‘isclose’, ‘isfinite’, ‘isinf’, ‘isnan’, ‘isqrt’, ‘lcm’, ‘ldexp’, ‘lgamma’, ‘log’, ‘log10’, ‘log1p’, ‘log2’, ‘modf’, ‘nan’, ‘nextafter’, ‘perm’, ‘pi’, ‘pow’, ‘prod’, ‘radians’, ‘remainder’, ‘sin’, ‘sinh’, ‘sqrt’, ‘tan’, ‘tanh’, ‘tau’, ‘trunc’, ‘ulp’]
ceil----------向上取整
floor----------向下取整
e() --------自然常数
fabs()---------求绝对值 abs-----全局函数 他们俩等价
fmod-------求模运算
isnan--------判断是不是数字(是数字返回False nan -----not number)
Isfinite----------判断是不是无限
pi---------圆周率
pow------幂次方
sqrt-----平方根
os模块
操作系统文件
清屏的方法----------import os os.system(“cls”)
[‘DirEntry’, ‘F_OK’, ‘GenericAlias’, ‘Mapping’, ‘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’, ‘PathLike’, ‘R_OK’, ‘SEEK_CUR’, ‘SEEK_END’, ‘SEEK_SET’, ‘TMP_MAX’, ‘W_OK’, ‘X_OK’, ‘_AddedDllDirectory’, ‘_Environ’, ‘all’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘_check_methods’, ‘_execvpe’, ‘_exists’, ‘_exit’, ‘_fspath’, ‘_get_exports_list’, ‘_walk’, ‘_wrap_close’, ‘abc’, ‘abort’, ‘access’, ‘add_dll_directory’, ‘altsep’, ‘chdir’, ‘chmod’, ‘close’, ‘closerange’, ‘cpu_count’, ‘curdir’, ‘defpath’, ‘device_encoding’, ‘devnull’, ‘dup’, ‘dup2’, ‘environ’, ‘error’, ‘execl’, ‘execle’, ‘execlp’, ‘execlpe’, ‘execv’, ‘execve’, ‘execvp’, ‘execvpe’, ‘extsep’, ‘fdopen’, ‘fsdecode’, ‘fsencode’, ‘fspath’, ‘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_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’, ‘unsetenv’, ‘urandom’, ‘utime’, ‘waitpid’, ‘waitstatus_to_exitcode’, ‘walk’, ‘write’]
chdir--------修改工作目录
curdir---------获取当前的目录 返回的结果是相对路径
(绝对路径:os.path.abspath(os.curdir))
chmod--------修改权限
close---------关闭文件的路径
cpu_count-------返回cpu的核对应的线程数
getcwd-------获取当前的路径(返回的是绝对路径)相当于linux下的pwd
getpid------获取当前进程的进程编号
getppid-------获取当前进程的父进程的进程编号
kill-------通过进程编号杀死进程
linesep------属性 对应系统下的换行符
listdir--------返回对应目录下所有的文件夹 包括隐藏的文件夹,返回的是列表
makedirs()-----创建目录,支持多层目录的创建
mkdir()-------只能创建一个目录
open()--------创建文件 open()全局函数 他们是一样的
pathsep-----获取环境变量分隔符windows ;linux :
sep------获取路径的分隔符
remove(文件名或者路径)------删除文件
removedirs(目录)------删除目录,支持多级删除(递归)
system---------执行终端命令
os.path模块
[‘all’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘_abspath_fallback’, ‘_get_bothseps’, ‘_getfinalpathname’, ‘_getfinalpathname_nonstrict’, ‘_getfullpathname’, ‘_getvolumepathname’, ‘_nt_readlink’, ‘_readlink_deep’, ‘abspath’, ‘altsep’, ‘basename’, ‘commonpath’, ‘commonprefix’, ‘curdir’, ‘defpath’, ‘devnull’, ‘dirname’, ‘exists’, ‘expanduser’, ‘expandvars’, ‘extsep’, ‘genericpath’, ‘getatime’, ‘getctime’, ‘getmtime’, ‘getsize’, ‘isabs’, ‘isdir’, ‘isfile’, ‘islink’, ‘ismount’, ‘join’, ‘lexists’, ‘normcase’, ‘normpath’, ‘os’, ‘pardir’, ‘pathsep’, ‘realpath’, ‘relpath’, ‘samefile’, ‘sameopenfile’, ‘samestat’, ‘sep’, ‘split’, ‘splitdrive’, ‘splitext’, ‘stat’, ‘supports_unicode_filenames’, ‘sys’]
Import os.path
Import os.path as p
from os import path
abspath(相对路径)-----------返回路径所对应的绝对路径
altsep--------查看python中的分隔符
basename----------返回文件名称
dirname---------文件目录
exists---------判断文件或目录是否存在
getctime----------获取时间(创建时间)
getmtime------------获取修改时间
getsize-----------获取文件大小,单位是字节
Isdir--------判断是不是目录(文件夹)
Isfile----------判断是不是文件
Isabs----------判断是不是绝对路径
Islink---------判断是不是链接
Ismount--------判断是不是挂载文件
join-----------拼接路径
sep----------路径分隔符
split---------分割路径
relpath----------返回真正的路径(相对路径)
练习:
结合os.path和os模块,以及函数的递归
给出一个路径 遍历当前路径所有文件和文件夹 打印出所有的文件(遇到文件输出路径,如果遇到文件夹继续遍历文件夹)
sys模块
[‘breakpointhook’, ‘displayhook’, ‘doc’, ‘excepthook’, ‘interactivehook’, ‘loader’, ‘name’, ‘package’, ‘spec’, ‘stderr’, ‘stdin’, ‘stdout’, ‘unraisablehook’, ‘_base_executable’, ‘_clear_type_cache’, ‘_current_frames’, ‘_debugmallocstats’, ‘_enablelegacywindowsfsencoding’, ‘_framework’, ‘_getframe’, ‘_git’, ‘_home’, ‘_xoptions’, ‘addaudithook’, ‘api_version’, ‘argv’, ‘audit’, ‘base_exec_prefix’, ‘base_prefix’, ‘breakpointhook’, ‘builtin_module_names’, ‘byteorder’, ‘call_tracing’, ‘copyright’, ‘displayhook’, ‘dllhandle’, ‘dont_write_bytecode’, ‘exc_info’, ‘excepthook’, ‘exec_prefix’, ‘executable’, ‘exit’, ‘flags’, ‘float_info’, ‘float_repr_style’, ‘get_asyncgen_hooks’, ‘get_coroutine_origin_tracking_depth’, ‘getallocatedblocks’, ‘getdefaultencoding’, ‘getfilesystemencodeerrors’, ‘getfilesystemencoding’, ‘getprofile’, ‘getrecursionlimit’, ‘getrefcount’, ‘getsizeof’, ‘getswitchinterval’, ‘gettrace’, ‘getwindowsversion’, ‘hash_info’, ‘hexversion’, ‘implementation’, ‘int_info’, ‘intern’, ‘is_finalizing’, ‘last_traceback’, ‘last_type’, ‘last_value’, ‘maxsize’, ‘maxunicode’, ‘meta_path’, ‘modules’, ‘path’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘platlibdir’, ‘prefix’, ‘ps1’, ‘ps2’, ‘pycache_prefix’, ‘set_asyncgen_hooks’, ‘set_coroutine_origin_tracking_depth’, ‘setprofile’, ‘setrecursionlimit’, ‘setswitchinterval’, ‘settrace’, ‘stderr’, ‘stdin’, ‘stdout’, ‘thread_info’, ‘unraisablehook’, ‘version’, ‘version_info’, ‘warnoptions’, ‘winver’]
api_version-------------获取python内部的版本号
argv()-------接收脚本参数 args
copyright----------输出cpython版本号
exit-----------退出系统
getdefaultencoding----------获取的是默认编码默认utf-8
getfilesystemencoding-----------获取文件系统的默认编码 默认utf-8
getrecursionlimit()----------获取python对递归的限制层数
setrecursionlimit-----------------重新设置递归的限制层数
getrefcout(对象)---------获取对象的引用计数,是垃圾回收机制里的引用计数,注意:任何对象都有一个引用计数
getwindowsversion--------获取当前窗口的版本信息
version--------获取版本信息
python中的垃圾回收机制:
以引用计数为主,以标记清除和分代收集为辅
Java 以标记清除为主,以引用计数和分代收集为辅