1.在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同。
2.UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。
3.时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。
4.元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。下面列出这种方式元组中的几个元素:

6 常用模块 (time,random,os,sys,shutil, json&pickle, shelve,xml,configparser,hashlib,suprocess,logging)

>>> import time

#时间戳 >>> print(time.time()) 1496901701.6700494

#结构化的时间 >>> print(time.localtime()) time.struct_time(tm_year=2017, tm_mon=6, tm_mday=8, tm_hour=14, tm_min=1, tm_sec =59, tm_wday=3, tm_yday=159, tm_isdst=0) >>> print(time.localtime().tm_year) 2017 >>> print(time.gmtime()) time.struct_time(tm_year=2017, tm_mon=6, tm_mday=8, tm_hour=6, tm_min=2, tm_sec= 23, tm_wday=3, tm_yday=159, tm_isdst=0)

#格式化的字符串 >>> print(time.strftime('%Y-%m-%d %H:%M:%S')) 2017-06-08 14:02:38 >>> print(time.strftime('%Y-%m-%d %X')) 2017-06-08 14:02:50 >>> print(time.localtime(13211123)) time.struct_time(tm_year=1970, tm_mon=6, tm_mday=3, tm_hour=5, tm_min=45, tm_sec =23, tm_wday=2, tm_yday=154, tm_isdst=0) >>> print(time.localtime(time.time())) time.struct_time(tm_year=2017, tm_mon=6, tm_mday=8, tm_hour=14, tm_min=3, tm_sec =20, tm_wday=3, tm_yday=159, tm_isdst=0) >>> print(time.mktime(time.localtime())) 1496901809.0 >>> print(time.strftime('%Y %X',time.localtime())) 2017 14:03:40 >>> print(time.strptime('2017-06-04 11:59:59','%Y-%m-%d %X')) time.struct_time(tm_year=2017, tm_mon=6, tm_mday=4, tm_hour=11, tm_min=59, tm_se c=59, tm_wday=6, tm_yday=155, tm_isdst=-1) >>> print(time.ctime(123123132)) Mon Nov 26 08:52:12 1973 >>> print(time.asctime(time.localtime())) Thu Jun 8 14:04:28 2017 >>>

二、random模块 

#随机选取一个,用于爬虫更换IP地址
>>> import random
>>> proxy_ip=[
...     '1.1.1.1',
...     '1.1.1.2',
...     '1.1.1.3',
...     '1.1.1.4',
... ]
>>>
>>> print(random.choice(proxy_ip))
1.1.1.1
>>> print(random.choice(proxy_ip))
1.1.1.2
>>> print(random.choice(proxy_ip))
1.1.1.3
>>> print(random.choice(proxy_ip))
1.1.1.3
#生产验证码
>>> def v_code(n=5):
...     res=''
...     for i in range(n):
...         num=random.randint(0,9)
...         s=chr(random.randint(65,90))
...         add=random.choice([num,s])
...         res+=str(add)
...     return res
...
>>> print(v_code(6))
CLI56J
>>> print(v_code(6))
AN6P59
>>> print(v_code(6))
UOAEB5

os模块  

 1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
 2 os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
 3 os.curdir  返回当前目录: ('.')
 4 os.pardir  获取当前目录的父目录字符串名:('..')
 5 os.makedirs('dirname1/dirname2')    可生成多层递归目录
 6 os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
 7 os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
 8 os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
 9 os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
10 os.remove()  删除一个文件
11 os.rename("oldname","newname")  重命名文件/目录
12 os.stat('path/filename')  获取文件/目录信息
13 os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
14 os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
15 os.pathsep    输出用于分割文件路径的字符串 win下为;,Linux下为:
16 os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
17 os.system("bash command")  运行shell命令,直接显示
18 os.environ  获取系统环境变量
19 os.path.abspath(path)  返回path规范化的绝对路径
20 os.path.split(path)  将path分割成目录和文件名二元组返回
21 os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
22 os.path.basename(path)  返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
23 os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
24 os.path.isabs(path)  如果path是绝对路径,返回True
25 os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
26 os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
27 os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
28 os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
29 os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间
30 os.path.getsize(path) 返回path的大小
View Code

相关文章:

  • 2021-07-22
  • 2022-12-23
  • 2022-12-23
  • 2022-03-09
  • 2021-07-09
  • 2022-12-23
  • 2022-01-10
猜你喜欢
  • 2022-12-23
  • 2021-04-20
  • 2021-04-01
  • 2021-08-10
  • 2022-12-23
  • 2021-08-31
  • 2021-08-08
相关资源
相似解决方案