码迷,mamicode.com
首页 > 编程语言 > 详细

python常用模块

时间:2017-11-05 14:30:13      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:class   目录名   https   管道   asi   anywhere   当前时间   expired   debug   

1Time

import time

print(time.time())
print(time.clock())#计算cpu执行时间
print(time.gmtime())#世界标准时间
print(time.strftime())  #Commonly used format codes:  设置时间第一个参数为设置时间格式
                            # %Y  Year with century as a decimal number.
                            # %m  Month as a decimal number [01,12].
                            # %d  Day of the month as a decimal number [01,31].
                            # %H  Hour (24-hour clock) as a decimal number [00,23].
                            # %M  Minute as a decimal number [00,59].
                            # %S  Second as a decimal number [00,61].
                            # %z  Time zone offset from UTC.
                            # %a  Locale‘s abbreviated weekday name.
                            # %A  Locale‘s full weekday name.
                            # %b  Locale‘s abbreviated month name.
                            # %B  Locale‘s full month name.
                            # %c  Locale‘s appropriate date and time representation.
                            # %I  Hour (12-hour clock) as a decimal number [01,12].
                            # %p  Locale‘s equivalent of either AM or PM.
print(time.strptime())#字符串时间转换为结构化时间 第一个参数为字符串第二个为时间格式
print(time.ctime())#当前时间格式固定 参数转化为标准时间从1970年开始算
print(time.mktime())#转换为时间戳
import datetime
print(datetime.datetime.now())#直接看时间

2logging模块

技术分享

日志格式:

技术分享

技术分享

 

 

 

技术分享
import logging

# logging.debug(‘debug message‘)
# logging.info(‘info message‘)
# logging.warning(‘warning message‘)
# logging.error(‘error message‘)
# logging.critical(‘critical message‘)
#
# logging.basicConfig(level=logging.DEBUG,#级别
#                     format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)‘,#格式
#                     datefmt=‘%a,%d %b %Y %H:%M:%S‘,#时间格式
#                     filename=‘/tmp/test.log‘,#文件目录名字
#                     filemode=‘w‘)#设置输出到文件

logger=logging.getLogger()
#创建一个handler,用于写入日志文件
fh=logging.FileHandler(test.log)
#再创建一个handler,用于输出控制台
ch=logging.StreamHandler()
logger.setLevel(logging.DEBUG)

formatter=logging.Formatter(%(asctime)s-%(name)s-%(levelname)s-%(message))
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
View Code

3OS模块

import os
os.getcwd()#获得工作目录
os.chdir()#更改当前脚本工作目录
os.curdir#当前路径
os.makedirs()#生成多层文件夹
os.removedirs()#删除多层文件夹有内容的文件夹不删除
os.mkdir()#生成文件夹只能创建一个文件夹
os.rmdir()#删除一个文件夹
os.listdir()#输出当前路径下的文件文件夹
os.remove()#删除一个文件
os.rename()#更改文件名
os.stat()#文件信息
os.sep()#输出操作系统指定的路径分隔符
os.linesep()#输出终止符(换行符
os.pathsep()#路径分隔符
os.system()#运行shell命令
os.environ#环境变量
os.path.abspath()#绝对路径
os.path.split()#对路径进行分割 路径 文件名
os.path.dirname()#上一个文件夹的名字
os.path.basename()#返回最后文件名

os.path.join()#路径拼接

4sys模块

import sys #与python解释器交互
# sys.argv()           #命令行参数List,第一个元素是程序本身路径
# sys.exit()        #退出程序,正常退出时exit(0)
print(sys.version)      #获取Python解释程序的版本信息
# sys.maxint()        #最大的Int值
# sys.path()           #返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
# sys.platform()       #返回操作系统平台名称
#sys.path.append()#添加路径
sys.stdout.write(please:)#标准输出

5hashlib模块

import  hashlib#加密模块
import time
m=hashlib.md5()
print(m)
t_start=time.time()
m.update(hello world.encode(utf-8))
time.sleep(2)
t_end=time.time()
print(t_end-t_start)
print(m.hexdigest())
s=hashlib.sha256()
s.update(hello world.encode(utf-8))
print(s.hexdigest())

6ConfigParser模块

import  configparser
config=configparser.ConfigParser()
# config[‘DEFAULT‘]={‘ServerAliveInterval‘:‘45‘,
#                    ‘Compression‘:‘yes‘,
#                    ‘CompressionLevel‘:‘9‘}
#
# config[‘bitbucket.org‘]={}
# config[‘bitbucket.org‘][‘User‘]=‘hg‘
# config[‘topsecret.server.com‘]={}
# topsecret=config[‘topsecret.server.com‘]
# topsecret[‘Host Port‘]=‘50022‘
# topsecret[‘ForwardX11‘]=‘no‘
# config[‘DEFAULT‘][‘ForwardX11‘]=‘yes‘
# with open(‘example.ini‘,‘w‘)as configfile:
#     config.write(configfile)
config.read(example.ini)
# print(config.sections())
# print(config.defaults())

# for key in config[‘bitbucket.org‘]:
#     print(key)
# config.remove_section(‘topsecret.server.com‘)


config.set(bitbucket.org,user,alex)
config.write(open(example.ini,w))

7json&pickle模块

json,用于字符串 和 python数据类型间进行转换

Json模块提供了四个功能:dumps、dump、loads、load

import json
dic={name:alex,age:18}
data=json.dumps(dic)#转换为json形式的仿字典
f=open(test,w)
f.write(data)
f.close()  #向文件写入1


dic={name:alex,age:18}
f=open(test,w)
import json
json.dump(dic,f)
f.close()#向文件写入2
import json

f=open(test,r)
# data=f.read()
# data=json.loads(data)#将json模式的仿字典转化为python形式的字典
data=json.load(f)
f.close()
print(data[name])  #从文件读取数据

pickle,用于python特有的类型 和 python的数据类型间进行转换

pickle模块提供了四个功能:dumps、dump、loads、load

技术分享

8shelve模块

import shelve
f=shelve.open(shelve_module)

# f[‘info‘]={‘name‘:‘alex‘,‘age‘:‘18‘}
data=f.get(info)
print(data)

代码运行后结果为

技术分享

9xml处理模块

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

xml的格式如下,就是通过<>节点来区别数据结构的:

技术分享
 1 <?xml version="1.0"?>
 2 <data>
 3     <country name="Liechtenstein">
 4         <rank updated="yes">2</rank>
 5         <year>2008</year>
 6         <gdppc>141100</gdppc>
 7         <neighbor name="Austria" direction="E"/>
 8         <neighbor name="Switzerland" direction="W"/>
 9     </country>
10     <country name="Singapore">
11         <rank updated="yes">5</rank>
12         <year>2011</year>
13         <gdppc>59900</gdppc>
14         <neighbor name="Malaysia" direction="N"/>
15     </country>
16     <country name="Panama">
17         <rank updated="yes">69</rank>
18         <year>2011</year>
19         <gdppc>13600</gdppc>
20         <neighbor name="Costa Rica" direction="W"/>
21         <neighbor name="Colombia" direction="E"/>
22     </country>
23 </data>
View Code

xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml 

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
 
#遍历xml文档
for child in root:
    print(child.tag, child.attrib)
    for i in child:
        print(i.tag,i.text)
 
#只遍历year 节点
for node in root.iter(year):
    print(node.tag,node.text)

修改和删除xml文档内容

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter(year):
    new_year = int(node.text) + 1
    node.text = str(new_year)
    node.set("updated","yes")
 
tree.write("xmltest.xml")
 
 
#删除node
for country in root.findall(country):
   rank = int(country.find(rank).text)
   if rank > 50:
     root.remove(country)
 
tree.write(output.xml)

自己创建xml文档

import xml.etree.ElementTree as ET
 
 
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = 33
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = 19
 
et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
 
ET.dump(new_xml) #打印生成的格式

10PyYAML模块

Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 

Subprocess模块 

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions:

os.system
os.spawn*

The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly.

The run() function was added in Python 3.5; if you need to retain compatibility with older versions, see the Older high-level API section.

subprocess.run(args*stdin=Noneinput=Nonestdout=Nonestderr=Noneshell=Falsetimeout=Nonecheck=False)

Run the command described by args. Wait for command to complete, then return a CompletedProcess instance.

The arguments shown above are merely the most common ones, described below in Frequently Used Arguments (hence the use of keyword-only notation in the abbreviated signature). The full function signature is largely the same as that of the Popen constructor - apart from timeoutinput and check, all the arguments to this function are passed through to that interface.

This does not capture stdout or stderr by default. To do so, pass PIPE for the stdout and/or stderr arguments.

The timeout argument is passed to Popen.communicate(). If the timeout expires, the child process will be killed and waited for. The TimeoutExpired exception will be re-raised after the child process has terminated.

The input argument is passed to Popen.communicate() and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string if universal_newlines=True. When used, the internal Popen object is automatically created withstdin=PIPE, and the stdin argument may not be used as well.

If check is True, and the process exits with a non-zero exit code, a CalledProcessError exception will be raised. Attributes of that exception hold the arguments, the exit code, and stdout and stderr if they were captured.

常用subprocess方法示例

#执行命令,返回命令执行状态 , 0 or 非0
>>> retcode = subprocess.call(["ls", "-l"])

#执行命令,如果命令结果为0,就正常返回,否则抛异常
>>> subprocess.check_call(["ls", "-l"])
0

#接收字符串格式命令,返回元组形式,第1个元素是执行状态,第2个是命令结果 
>>> subprocess.getstatusoutput(‘ls /bin/ls‘)
(0, ‘/bin/ls‘)

#接收字符串格式命令,并返回结果
>>> subprocess.getoutput(‘ls /bin/ls‘)
‘/bin/ls‘

#执行命令,并返回结果,注意是返回结果,不是打印,下例结果返回给res
>>> res=subprocess.check_output([‘ls‘,‘-l‘])
>>> res
b‘total 0\ndrwxr-xr-x 12 alex staff 408 Nov 2 11:05 OldBoyCRM\n‘

#上面那些方法,底层都是封装的subprocess.Popen
poll()
Check if child process has terminated. Returns returncode

wait()
Wait for child process to terminate. Returns returncode attribute.


terminate() 杀掉所启动进程
communicate() 等待任务结束

stdin 标准输入

stdout 标准输出

stderr 标准错误

pid
The process ID of the child process.

#例子
>>> p = subprocess.Popen("df -h|grep disk",stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
>>> p.stdout.read()
b‘/dev/disk1 465Gi 64Gi 400Gi 14% 16901472 104938142 14% /\n‘

 

>>> subprocess.run(["ls", "-l"])  # doesn‘t capture output
CompletedProcess(args=[ls, -l], returncode=0)
 
>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command exit 1 returned non-zero exit status 1
 
>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=[ls, -l, /dev/null], returncode=0,
stdout=bcrw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n)

调用subprocess.run(...)是推荐的常用方法,在大多数情况下能满足需求,但如果你可能需要进行一些复杂的与系统的交互的话,你还可以用subprocess.Popen(),语法如下:

p = subprocess.Popen("find / -size +1000000 -exec ls -shl {} \;",shell=True,stdout=subprocess.PIPE)
print(p.stdout.read())

可用参数:

    • args:shell命令,可以是字符串或者序列类型(如:list,元组)
    • bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
    • stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
    • preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
    • close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
      所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
    • shell:同上
    • cwd:用于设置子进程的当前目录
    • env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
    • universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
    • startupinfo与createionflags只在windows下有效
      将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等

终端输入的命令分为两种:

    • 输入即可得到输出,如:ifconfig
    • 输入进行某环境,依赖再输入,如:python

需要交互的命令示例

import subprocess
 
obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write(print 1 \n )
obj.stdin.write(print 2 \n )
obj.stdin.write(print 3 \n )
obj.stdin.write(print 4 \n )
 
out_error_list = obj.communicate(timeout=10)
print out_error_list

subprocess实现sudo 自动输入密码

import subprocess
 
def mypass():
    mypass = 123 #or get the password from anywhere
    return mypass
 
echo = subprocess.Popen([echo,mypass()],
                        stdout=subprocess.PIPE,
                        )
 
sudo = subprocess.Popen([sudo,-S,iptables,-L],
                        stdin=echo.stdout,
                        stdout=subprocess.PIPE,
                        )
 
end_of_pipe = sudo.stdout
 
print "Password ok \n Iptables Chains %s" % end_of_pipe.read()

 

python常用模块

标签:class   目录名   https   管道   asi   anywhere   当前时间   expired   debug   

原文地址:http://www.cnblogs.com/leesen934/p/7787303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!