时间模块

import time
# print(time.time())       #时间戳
# print(time.strftime('%Y-%m-%d %X'))    #格式化字符
# print(time.strftime('%Y-%m-%d  %H-%M-%S'))

# print(time.localtime())    #时间字符串
# print(time.struct_time)

#时间戳是计算机能够识别的时间,格式化时间字符串是人能看的时间,元组用来操作时间

# print(time.localtime(time.time()))       #时间戳---->元组时间
# print(time.mktime(time.localtime()))     #元组时间--->时间戳
# print(time.strptime('2017-08-08','%Y-%m-%d'))      #格式化时间----->元组时间
# print(time.strftime('%Y-%m-%d %D'))         #元组时间------>格式化时间
# print(time.strftime('%Y-%m-%d',time.localtime(1224432325)))         #元组时间------>格式化时间

# print(time.asctime(time.localtime(1500000000)))      #时间字符串----->格式化字符串   某一时间
# print(time.asctime())       #当前时间

# print(time.ctime())
# print(time.ctime(1500000000))
模块:time,random,os,sys

 



random
import random
#随机小数
# print(random.random())       #大于0小于1的随机小数
# print(random.uniform(1,7))     #大于1小于7的随机小数

# 随机整数
# print(random.randint(1,7))       #1-7之间的随机整数
# print(random.randrange(1,10,2))    #1-10之间的随机奇数

#随机返回
# print(random.choice([1,2,3,4]))      #返回一个
# print(random.sample([1,'23',[4,5]],3))    #返回多个,个数为第二个参数,第二个参数不返回
# print(random.sample([1,2,3,5],4))

# 打乱列表顺序
# item = [1,2,3,4,5]
# random.shuffle(item)     #没有返回值
# print(item)
产生随机数
 1 def v_code():
 2     code = ''
 3     for i in range(8):      #产生8个字符
 4         num = random.randint(0,9)      #num取0-9的数字,头尾都顾
 5         alf = chr(random.randint(65,90))
 6         alp = chr(random.randint(97,122))
 7         add = random.choice([num,alf,alp])   #每次选择一个加进去
 8         code= ''.join([code,str(add)])     #拼接
 9         print(code)
10     return code
11 print(v_code())
View Code

 

os模块

'''
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
os.curdir  返回当前目录: ('.')
os.pardir  获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')    可生成多层递归目录
os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname')    生成单级目录;相当于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下为"\t\n",Linux下为"\n"
os.pathsep    输出用于分割文件路径的字符串 win下为;,Linux下为:
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command")  运行shell命令,直接显示
os.popen("bash command)  运行shell命令,获取执行结果
os.environ  获取系统环境变量


os.path
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所指向的文件或者目录的最后修改时间
os.path.getsize(path) 返回path的大小
'''
View Code

相关文章:

  • 2022-02-19
  • 2022-12-23
  • 2021-07-22
  • 2021-08-08
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-19
  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2021-07-30
相关资源
相似解决方案