1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 #一 time模块
5 import time
6 #时间戳
7 #print(time.time())
8 #格式化的时间字符串
9 #print(time.strftime("%Y-%m-%d %X"))
10 #本地时间区的struct_time
11 #print(time.localtime())
12 #utc时区的struct_time
13 #print(time.gmtime())
14 #将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准
15 # print(time.localtime())
16 # print(time.localtime(time.time()))
17 ## gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
18 # mktime(t) : 将一个struct_time转化为时间戳。
19 #print(time.mktime(time.localtime()))
20 #strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个元素越界,ValueError的错误将会被抛出。
21 #print(time.strftime("%Y-%m-%d %X",time.localtime()))
22 # time.strptime(string[, format])
23 #把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
24 #print(time.strptime('2017-06-03 00:49:29', '%Y-%m-%d %X'))
25 #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。
26 #asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。
27 #如果没有参数,将会将time.localtime()作为参数传入
28 #print(time.asctime())
29 #ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
30 #print(time.ctime())
31 #print(time.ctime(time.time()))
32 #其他用法
33 #sleep(secs)
34 #线程推迟指定的时间运行,单位为秒。
35 #二 random模块
36 import random
37 #(0,1)----float 大于0且小于1之间的小数
38 #print(random.random())
39 ##[1,3] 大于等于1且小于等于3之间的整数
40 #print(random.randint(1,3))
41 #大于等于1且小于3之间的整数
42 #print(random.randrange(1,3))
43 #1或者23或者[4,5]
44 #print(random.choice([1,'23',[4,5]]))
45 #列表元素任意2个组合
46 #print(random.sample([1,'23',[4,5]],2))
47 #大于1小于3的小数,如1.927109612082716
48 #print(random.uniform(1,3))
49 #打乱item的顺序,相当于"洗牌"
50 # item=[1,3,5,7,9]
51 # random.shuffle(item)
52 # print(item)
53
54 #三 os模块
55 # os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
56 # os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd
57 # os.curdir 返回当前目录: ('.')
58 # os.pardir 获取当前目录的父目录字符串名:('..')
59 # os.makedirs('dirname1/dirname2') 可生成多层递归目录
60 # os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
61 # os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname
62 # os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
63 # os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
64 # os.remove() 删除一个文件
65 # os.rename("oldname","newname") 重命名文件/目录
66 # os.stat('path/filename') 获取文件/目录信息
67 # os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
68 # os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
69 # os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为:
70 # os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
71 # os.system("bash command") 运行shell命令,直接显示
72 # os.environ 获取系统环境变量
73 # os.path.abspath(path) 返回path规范化的绝对路径
74 # os.path.split(path) 将path分割成目录和文件名二元组返回
75 # os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素
76 # os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
77 # os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False
78 # os.path.isabs(path) 如果path是绝对路径,返回True
79 # os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False
80 # os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False
81 # os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
82 # os.path.getatime(path) 返回path所指向的文件或者目录的最后存取时间
83 # os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间
84 # os.path.getsize(path) 返回path的大小
85 # import os
86 # import os,sys
87 # possible_topdir = os.path.normpath(os.path.join(
88 # os.path.abspath(__file__),
89 # os.pardir, #上一级
90 # os.pardir,
91 # os.pardir
92 # ))
93 # sys.path.insert(0,possible_topdir)
94 #四 sys模块
95 import sys
96 #sys.argv 命令行参数List,第一个元素是程序本身路径
97 #sys.exit(n) 退出程序,正常退出时exit(0)
98 #print(sys.version) 获取Python解释程序的版本信息
99 #sys.maxint 最大的Int值
100 #sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
101 #print(sys.platform) 返回操作系统平台名称
102
103 #五 shutil模块
104 #高级的 文件、文件夹、压缩包 处理模块
105 import shutil
106 #将文件内容拷贝到另一个文件中
107 #shutil.copyfileobj(fsrc, fdst[, length])
108 #shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
109 #拷贝文件
110 #shutil.copyfile(src, dst)
111 #shutil.copyfile('f1.log', 'f2.log') #目标文件无需存在
112 #shutil.copymode(src, dst)
113 #仅拷贝权限。内容、组、用户均不变
114 #shutil.copymode('f1.log', 'f2.log') #目标文件必须存在
115 #shutil.copystat(src, dst)
116 #仅拷贝状态的信息,包括:mode bits, atime, mtime, flags
117 #shutil.copystat('f1.log', 'f2.log') #目标文件必须存在
118 #shutil.copy(src, dst)
119 #拷贝文件和权限
120 #shutil.copy('f1.log', 'f2.log')
121 #拷贝文件和状态信息
122 #shutil.copy2(src, dst)
123 #shutil.ignore_patterns(*patterns)
124 #shutil.copytree(src, dst, symlinks=False, ignore=None)
125 #递归的去拷贝文件夹
126 #shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除
127 #软链、通常的拷贝都把软连接拷贝成硬链接,即对待软连接来说,创建新的文件
128 #shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
129 #shutil.rmtree(path[, ignore_errors[, onerror]])
130 #递归的去删除文件
131 #shutil.rmtree('folder1')
132 #shutil.make_archive(base_name, format,...)
133 #
134 # 创建压缩包并返回文件路径,例如:zip、tar
135 #
136 # 创建压缩包并返回文件路径,例如:zip、tar
137 #
138 # base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
139 # 如 data_bak =>保存至当前路径
140 # 如:/tmp/data_bak =>保存至/tmp/
141 # format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
142 # root_dir: 要压缩的文件夹路径(默认当前目录)
143 # owner: 用户,默认当前用户
144 # group: 组,默认当前组
145 # logger: 用于记录日志,通常是logging.Logger对象
146 #shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:
147 # import zipfile
148 #
149 # # 压缩
150 # z = zipfile.ZipFile('laxi.zip', 'w')
151 # z.write('a.log')
152 # z.write('data.data')
153 # z.close()
154 #
155 # # 解压
156 # z = zipfile.ZipFile('laxi.zip', 'r')
157 # z.extractall(path='.')
158 # z.close()
159
160 # import tarfile
161 #
162 # # 压缩
163 # >>> t=tarfile.open('/tmp/egon.tar','w')
164 # >>> t.add('/test1/a.py',arcname='a.bak')
165 # >>> t.add('/test1/b.py',arcname='b.bak')
166 # >>> t.close()
167 #
168 #
169 # # 解压
170 # >>> t=tarfile.open('/tmp/egon.tar','r')
171 # >>> t.extractall('/egon')
172 # >>> t.close()
173
174 # import json
175 # x="[null,true,false,1]"
176 # print(json.loads(x))
177 #re模块
178 # 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法。或者说:正则就是用来描述一类事物的规则。(在Python中)它内嵌在Python中,并通过 re 模块实现。正则表达式模式被编译成一系列的字节码,然后由用 C 编写的匹配引擎执行。
179 #
180 # 生活中处处都是正则:
181 #
182 # 比如我们描述:4条腿
183 #
184 # 你可能会想到的是四条腿的动物或者桌子,椅子等
185 #
186 # 继续描述:4条腿,活的
187 #
188 # 就只剩下四条腿的动物这一类了
189 #
190 # 二:常用匹配模式(元字符)
191 #
192 # http://blog.csdn.net/yufenghyc/article/details/51078107