Fifth, the method of introducing the module and common module

The method of introducing the module:
1) import module name
2) from the module name import sayhi
from os import system (system call from the OS module method, can use the system commands)
Example: system ( 'df -h')
3) import the module name as the new name
 
Common Module and Method:
OS (System Module)
 
sys (includes Python system environment variables, etc.)
sys.path (see Python's path environment variable)
 
Tab (for command completion, non-built-in system)
 
Achieve a certain function, and is a collection of all the code implemented
Modules exist in two ways: .py files "folder"
Modules:
1. Write your own (custom)
2. written by someone else (third party)
3.python write (built-in)
===========================================================
Import module:
import sys
from sys import argv
from sys import argv as test
===========================================================
Built-in modules:
A .os: for providing operating system level
os.getcwd () to get the current working directory that the current directory path python script work
os.chdir ( "dirname") script to change the current working directory; cd at the equivalent of shell
os.curdir Returns the current directory: ( '.')
os.pardir get the current directory's parent directory string name :( '..')
os.makedirs ( 'dirname1 / dirname2') may generate a multilayer recursive directory
os.removedirs ( 'dirname1') If the directory is empty, delete, and recursively to the parent directory, should also empty, delete, and so on
os.mkdir ( 'dirname') to generate a single level directory; is equivalent to the shell mkdir dirname
os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove() 删除一个文件
os.rename("oldname","newname") 重命名文件/目录
os.stat('path/filename') 获取文件/目录信息
os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep 输出当前平台使用的行终止符,win下为"\r\n",Linux下为"\n"
os.pathsep 输出用于分割文件路径的字符串
os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.environ 获取系统环境变量
os.path.abspath(path) 返回path规范化的绝对路径
os.path.split(path) 将path分割成目录和文件名二元组返回
os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) 如果path是绝对路径,返回True
os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
=======================================================================================================================
=======================================================================================================================
=======================================================================================================================
二.sys:用于提供对解释器相关的操作
sys.argv 命令行参数List,第一个元素是程序本身路径
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxint 最大的Int值
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]
=======================================================================================================================
=======================================================================================================================
=======================================================================================================================
三.ConfigParser:用于对特定格式(如my.cnf)的配置文件进行操作,当前模块的名称在 python 3.x 版本中变更为 configparser。
如我们经常用的MySQL的配置文件my.cnf:
# 注释1
; 注释2
 
[mysqld]
user = root
port = 3306
 
[client]
k1 = v1
 
----------------------------------------------------------------
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('my.cnf')
 
# ########## 读 ##########
#获取[mysqld],[client]======》得到了['mysqld','client']
#secs = config.sections()
#print secs
 
#获取[mysqld]下面的key======》得到了['user','port']
#options = config.options('mysqld')
#print options
 
#获取[mysqld]下面的数据======》得到了[('user','root'),('port','3306')]
#item_list = config.items('mysqld')
#print item_list
 
#获取[mysqld]下面的某一个值======》得到了root
#val = config.get('mysqld','user')
#获取[mysqld]下面的某一个值======》得到了root
#val = config.getint('mysqld','user') 相当于拿到值之后将其转换为int形态,比如你得到的值不是root而是123,默认它是字符串型,这时候你就可以用这条语句
 
# ########## 改写 ##########
#删除mysqld这个组的数据
#sec = config.remove_section('mysqld') =====》这个仅仅是在内存里删除了
#config.write(open('mysql.cnf', "w")) =====》这一句才是真正在文件里删除了
 
添加mysqld这个组
#sec = config.has_section('mysqld')
#sec = config.add_section('mysqld')
#config.write(open('my.cnf', "w"))
 
#更改内容
#config.set('mysqld','user',‘rooney’)
#config.write(open('my.cnf', "w"))
 
#删除某项的值
#config.remove_option('mysqld','port')
#config.write(open('my.cnf', "w"))
=======================================================================================================
=======================================================================================================
=======================================================================================================
四.hashlib:用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法
import hashlib
 
# ######## md5 ########
 
hash = hashlib.md5()
hash.update('admin')
print hash.hexdigest()
 
# ######## sha1 ########
 
hash = hashlib.sha1()
hash.update('admin')
print hash.hexdigest()
 
# ######## sha256 ########
 
hash = hashlib.sha256()
hash.update('admin')
print hash.hexdigest()
 
 
# ######## sha384 ########
 
hash = hashlib.sha384()
hash.update('admin')
print hash.hexdigest()
 
# ######## sha512 ########
 
hash = hashlib.sha512()
hash.update('admin')
print hash.hexdigest()
 
以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。
import hashlib
# ######## md5 ########
hash = hashlib.md5('rooney') =====>这个rooney就是自定义的key
hash.update('admin')
print hash.hexdigest()
还不够吊?python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密
 
 
import hmac
h = hmac.new('wueiqi')
h.update('hellowo')
print h.hexdigest()
不能再牛逼了!!!
=============================================================================================

Guess you like

Origin www.cnblogs.com/steven9898/p/11329327.html