- os、sys、hashlib、time、datetime、math、random等等
- 包和模块
包:package
我们可以简单理解文件夹就是包,但是注意,包不是文件夹
模块:module
xxx.py
import mypackage.b
或者
from mypackage import b
包 模块
- os模块 操作系统的软件和硬件
['abc', 'abort', 'access', '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', 'urandom',
'utime', 'waitpid', 'walk', 'write']
os.cpu_count() 显示cpu线程数
os.curdir 返回当前目录
os.getpid() 获取当前进程编号
os.getppid() 获取当前进程的父进程编号
os.listdir() 遍历当前路径
os.getcwd() 返回当前目录(绝对路径)
os.mkdir() 创建目录
os.makedirs() 使用递归创建多级目录
os.rmdir() 删除目录,只能删除一层
os.remove(文件) 删除文件
os.removedirs(path) 可以删除多个路径,注意,路径中不能存在文件
os.system(command) 运行一个command命令
注意:几种常见的导包方式
os.path 系统文件项 file system
from os import path
import os.path
import xxx.xxx.xxxx
import xxx.xxx.xxxx as alias
from xxx import *
from xxx ipmort xxx
- os.path模块 文件系统
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__', '_abspath_fallback', '_get_bothseps',
'_getfinalpathname', '_getfullpathname', '_getvolumepathname', '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']
os.path.abspath(相对路径) 通过相对路径返回出绝对路径
os.path.basename(路径) 获取文件名称,如果是目录,则最后目录的名称
os.path.dirname(路径) 文件的目录,如果是目录,则获取上级目录
其实质:os.path.abspath= os.path.dirname(路径) + os.path.basename(路径)
os.path.jion(路径,文件) 拼接路径和文件
os.path.exists(路径) 路径是否存在
os.path.isdir() 判断是否是目录
os.path.isfile() 判断是否是文件
os.path.islink() 判断是否是链接
os.path.split(path) 拆分path中的路径和文件名称
import os
from os import path
# 使用path,遍历路径下的所有文件
def scanner_files(url):
lists= os.listdir(url) #遍历当前路径
for f in lists:
real_path = os.path.join(url,f) # 将路径拼接起来
if path.isfile(real_path):
print(path.abspath(real_path)) #通过相对路径返回出绝对路径
else:
#不是文件,是目录
#递归的使用
scanner_files(real_path)
scanner_files("C:/")
- sys模块
import sys
sys.argv 返回的是一个列表,元素是脚本的参数
sys.exit() 退出系统
sys.getdefaultencoding() 获取系统的默认
sys.getrecursionlimit() 获取递归的最深层数(默认最多递归到1000)
sys.getrefcount() 获取程序引用技术的数量
5. math模块
6. hashlib模块
7. hmac 散列加密
加密——是否可逆
可逆加密
对称加密 加密解密使用同一个密钥
非对称加密 加密解密使用不同的密钥
不可逆加密 hash加密,散列加密 MD3 MD4 MD5 sha256
- 标准加密
import hashlib
pwd = input("请输入你的密码:")
md5=hashlib.md5(pwd.encode("utf-8"))
#盐值的混淆
md5.update('zhangsan'.encode('utf-8')) #123456zhangsan
new_pwd = md5.hexdigest()
print("加密后的密码是:",new_pwd)
- 非标准加密
import hashlib ,hmac
def get_hash_by_md5(msg,salt='zhangsan'):
md5=hashlib.md5(md5.encode('utf-8'))
md5.update(salt.encode('utf-8'))
new_pwd=md5.hexdigest()
return new_pwd
pwd=input('请输入你的密码:')
new_pwd=hmac.new(pwd.encode('utf-8'),'zhangsan'.encode('utf-8)','MD5')).hexdigest()
print('加密后的密码是:',new_pwd)