模块

发布时间 2023-12-02 01:19:21作者: 我才是最帅的那个男人
一,常用模块
二,random
  • 取随机小数 : 数学计算
1 # print(random.random()) # 取0-1之间的小数
2 # print(random.uniform(1,2)) # 取1-2之间的小数

  • 取随机整数 : 彩票 抽奖
1 # print(random.randint(1,2)) # [1,2]
2 # print(random.randrange(1,2)) # [1,2)
3 # print(random.randrange(1,200,2)) # [1,2)

 

  • 从一个列表中随机抽取值 : 抽奖
1 # l = ['a','b',(1,2),123]
2 # print(random.choice(l))
3 # print(random.sample(l,2))

应用:验证码

 

 1 def code(n = 6,alpha = True):
 2 #     s = ''
 3 #     for i in range(n):
 4 #         num = str(random.randint(0,9))
 5 #         if alpha:
 6 #             alpha_upper = chr(random.randint(65,90))
 7 #             alpha_lower = chr(random.randint(97,122))
 8 #             num = random.choice([num,alpha_upper,alpha_lower])
 9 #         s += num
10 #     return s
11 # print(code(4,False))
12 # print(code(alpha=False))

 

三,时间模块

time.sleep(2)  # 程序走到这儿会等待2s钟

时间格式:

  • '2018-8-20' '2018.8.20' 字符串数据类型
  • 结构化时间
  • 1534732642.617272 浮点型数据类型,以s为单位,给机器计算用的


# 格式化时间
# print(time.strftime('%Y-%m-%d %H:%M:%S')) # str format time
# print(time.strftime('%y-%m-%d %H:%M:%S')) # str format time
# print(time.strftime('%c'))

# 结构化时间
# struct_time = time.localtime() # 北京时间
# print(struct_time)
# print(struct_time.tm_mon)

# 时间戳换成字符串时间
# print(time.time())
# struct_time = time.localtime(1500000000)
# # print(time.gmtime(1500000000))
# ret = time.strftime('%y-%m-%d %H:%M:%S',struct_time)
# print(ret)

# 字符串时间 转 时间戳
# struct_time = time.strptime('2018-8-8','%Y-%m-%d')
# print(struct_time)
# res = time.mktime(struct_time)
# print(res)

 时间模块应用

 1 # 1.查看一下2000000000时间戳时间表示的年月日
 2 # 时间戳 - 结构化 - 格式化
 3 # struct_t = time.localtime(2000000000)
 4 # print(struct_t)
 5 # print(time.strftime('%y-%m-%d',struct_t))
 6 
 7 # 2.将2008-8-8转换成时间戳时间
 8 # t = time.strptime('2008-8-8','%Y-%m-%d')
 9 # print(time.mktime(t))
10 
11 # 3.请将当前时间的当前月1号的时间戳时间取出来 - 函数
12 # 2018-8-1
13 # def get_time():
14 #     st = time.localtime()
15 #     st2 = time.strptime('%s-%s-1'%(st.tm_year,st.tm_mon),'%Y-%m-%d')
16 #     return time.mktime(st2)
17 # print(get_time())
18 
19 # 4.计算时间差 - 函数
20     # 2018-8-19 22:10:8 2018-8-20 11:07:3
21     # 经过了多少时分秒
22 # str_time1 = '2018-8-19 22:10:8'
23 # str_time2 = '2018-8-20 11:07:3'
24 # struct_t1 = time.strptime(str_time1,'%Y-%m-%d %H:%M:%S')
25 # struct_t2 = time.strptime(str_time2,'%Y-%m-%d %H:%M:%S')
26 # timestamp1 = time.mktime(struct_t1)
27 # timestamp2 = time.mktime(struct_t2)
28 # sub_time = timestamp2 - timestamp1
29 # gm_time = time.gmtime(sub_time)
30 # # 1970-1-1 00:00:00
31 # print('过去了%d年%d月%d天%d小时%d分钟%d秒'%(gm_time.tm_year-1970,gm_time.tm_mon-1,
32 #                                  gm_time.tm_mday-1,gm_time.tm_hour,
33 #                                  gm_time.tm_min,gm_time.tm_sec))
View Code

四,sys模块

  sys模块是控制pycharm的一个模块。

  常用的分为三个:

  • sys.argv
    1 print(sys.argv)  # argv的第一个参数 是python这个命令后面的值
    2 # usr = input('username')
    3 # pwd = input('password')
    4 # usr = sys.argv[1] #因为第一个位置是文件地址,所以是从1开始的
    5 # pwd = sys.argv[2]
    6 # if usr == 'alex' and pwd == 'alex3714':
    7 #     print('登录成功')
    8 # else:
    9 #     exit()
    应用于:
    1. 程序员 运维人员 在命令行运行代码
    2. 操作系统input事件 阻塞 退出了CPU的竞争
  • sys.path
    1 # sys.path
    2 # print(sys.path)
    3 # 模块是存在解释器里的么??? 不是
    4 # 模块应该是存在硬盘上
    5 # 但是我在使用的时候 import --> 这个模块才到内存中
    6 
    7 # 一个模块能否被顺利的导入 全看sys.path下面有没有这个模块所在的
    8 # 自定义模块的时候 导入模块的时候 还需要再关注 sys.path
  • sys.modules
    1 # sys.modules
    2 # print(sys.modules)  # 是我们导入到内存中的所有模块的名字 : 这个模块的内存地址
    3 # print(sys.modules['re'].findall('\d','abc126'))

 

五,sys模块,是与操作系统进行交互的

 

与文件夹的创建等操作有关 
1
os.makedirs('dirname1/dirname2') 可生成多层递归目录 2 os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 3 os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirname 4 os.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname 5 os.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 6 os.remove() 删除一个文件 7 os.rename("oldname","newname") 重命名文件/目录 8 os.stat('path/filename') 获取文件/目录信息 9
与执行操作系统的命令有关 10 os.system("bash command") 运行shell命令,直接显示 11 os.popen("bash command).read() 运行shell命令,获取执行结果 12 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 13 os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cd 14

与路径的位置有关 15 os.path 16 os.path.abspath(path) 返回path规范化的绝对路径 17 os.path.split(path) 将path分割成目录和文件名二元组返回 18 os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 19 os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 20 os.path.exists(path) 如果path存在,返回True;如果path不存在,返回False 21 os.path.isabs(path) 如果path是绝对路径,返回True 22 os.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回False 23 os.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回False 24 os.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 25 os.path.getatime(path) 返回path所指向的文件或者目录的最后访问时间 26 os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间 27 os.path.getsize(path) 返回path的大小

 

 1 stat 结构:
 2 
 3 st_mode: inode 保护模式
 4 st_ino: inode 节点号。
 5 st_dev: inode 驻留的设备。
 6 st_nlink: inode 的链接数。
 7 st_uid: 所有者的用户ID。
 8 st_gid: 所有者的组ID。
 9 st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
10 st_atime: 上次访问的时间。
11 st_mtime: 最后一次修改的时间。
12 st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。
1 os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
2 os.linesep    输出当前平台使用的行终止符,win下为"\r\n",Linux下为"\n"
3 os.pathsep    输出用于分割文件路径的字符串 win下为;,Linux下为:
4 os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix

应用

一,验证码

 1 def yanzhengma(n=6,alp=True):
 2     wa=""
 3     for i in range(n):
 4         ws=str(random.randint(0,9))
 5         if alp:
 6             ws_upper=chr(random.randint(65,90))
 7             w=random.choice([ws_upper,ws])
 8             wa+=w
 9     return wa
10 print(yanzhengma(5,True))

 

二·,发红包

三,计算时间差函数

四,sys.argv 登录验证

五,os模块:

  #用户登录,登录之后 给这个用户创建一个属于他的文件夹 以用户名命名

  #如果用户已经注销,删除这个文件夹