一、random模块

1.随机取小数     (数学计算)

print(random.random())  #取0-1之间的小数
print(random.uniform(3,6))   #uniform(n,m)取一个范围之间的小数

2.取随机整数       (抽奖)

print(random.randint(1,2))  #顾头顾尾 或 [1,2]
print(random.randrange(1,2))  #顾头不顾尾 [1,2)
print(random.randrange(1,100,2)) #取1-99之间的奇数

3.从一个列表中随机抽取值     (抽奖)

lis = ['a','b',(1,2,3),123,456]
print(random.choice(lis)) #从列表中随机抽取一项元素
lis = ['a','b',(1,2,3),123,456]
print(random.sample(lis,2)) #从列表中随机抽取两项元素
choice直接用两次与sample一次取两个元素的区别:
lis = ['a','b',(1,2,3),123,456]
print(random.choice(lis))
print(random.choice(lis))
print(random.sample(lis,2))

choice两次取到的值可能是相同的,而sample一次取到的两个元素是不相同的,也就是说:sample取到的值是不重复的

4.打乱一个列表的顺序,在原来列表的基础上进行修改,节省空间          (洗牌)

random.shuffle(lis)
print(lis)

二、time模块

1.时间戳时间

print(time.time())

2.结构化时间

print(time.strftime("%Y-%m-%d"))
print(time.strftime("%y-%m-%d"))
print(time.strftime('%c'))

3.时间戳时间转换成字符串时间(格式化时间)

cishi = time.time()      #此刻的时间戳
struct_time = time.localtime(cishi) #将时间戳时间转成结构化时间
timestamp = time.strftime("%Y-%m-%d %H:%M:%S",struct_time) #将结构化时间转成格式化时间
print(timestamp)

4.将字符串时间转换成时间戳时间

struct_time = time.strptime("2018-8-20","%Y-%m-%d")   #将格式化时间转换成结构化时间
print(time.mktime(struct_time)) #再转换成时间戳时间

5.练习题

(1) 查看一下2000000000时间戳时间表示的年月日

struct_time = time.localtime(2000000000)
result = time.strftime("%Y-%m-%d",struct_time)
print(result)

(2) 将2018-8-20换成时间戳时间

struct_time = time.strptime("2018-8-20","%Y-%m-%d")
print(time.mktime(struct_time))

(3) 请将当前时间的当前月1号的时间戳时间取出来 - 函数

def get_time():
struct_time = time.localtime()
ret = time.strptime("%s-%s-1"%(struct_time.tm_year,struct_time.tm_mon),"%Y-%m-%d")
return time.mktime(ret)
print(get_time())

(4) 计算时间差 - 函数 2018-8-19 22:10:8 2018-8-20 11:07:3 经过了多少时分秒

t1 = '2018-8-19 22:10:8'
t2 = '2018-8-20 11:07:3'
struct_time1 = time.strptime(t1,'%Y-%m-%d %H:%M:%S')
struct_time2 = time.strptime(t2,'%Y-%m-%d %H:%M:%S')
timestamp1 = time.mktime(struct_time1)
timestamp2 = time.mktime(struct_time2)
result = timestamp2-timestamp1
gm_time = time.gmtime(result) #伦敦时间
print('过去了%d年%d月%d天%d小时%d分钟%d秒'%(gm_time.tm_year-1970,gm_time.tm_mon-1, gm_time.tm_mday-1,gm_time.tm_hour,gm_time.tm_min,gm_time.tm_sec))

三、sys模块 (是和python解释器打交道的)

1.sys.argv

print(sys.argv)   #argv是python这个命令后面的值
usr = sys.argv[1]
pwd = sys.argv[2]
if usr == 'alex' and pwd=='alex3741':
print("登陆成功")
else:
exit()

2.sys.path
3.sys.modules

print(sys.modules)
print(sys.modules['re'].findall("\d+","abc123")) #我们导入内存中所有模块的名字,这个模块的内存地址

四、os模块  (是和操作系统交互的模块)

1.

os.makedirs('dir1/dir2')
os.mkdir('dir3')
os.mkdir('dir3/dir4')

2.只能删空文件夹

os.rmdir('dir3/dir4')
os.removedirs('dir3/dir4')
os.removedirs('dir1/dir2')

3.程序要处理这些路径

ret = os.popen('dir) # 是和做查看类的操作
s =ret.read()
print(s)
print(s.split('\n'))
 os.listdir / os.path.join
file_lst = os.listdir('D:\sylar\s15')
for path in file_lst:
print(os.path.join('D:\sylar\s15',path))

print('-->',os.getcwd())  # current work dir 当前工作目录
并不是指当前文件所在的目录
当前文件是在哪个目录下执行的
 ret = os.popen('dir') # 是和做查看类的操作
 s =ret.read()
 print(s)

os.chdir('D:\sylar\s15\day18')  # 切换当前的工作目录
ret = os.popen('dir') # 是和做查看类的操作
s =ret.read()
print(s)

random模块、time模块、sys模块、os模块的更多相关文章

  1. python note 17 random、time、sys、os模块

    1.random模块(取随机数模块) # 取随机小数 : 数学计算 import random print(random.random())# 取0-1之间的小数 print(random.unifo ...

  2. python中sys和os模块的使用

    在python中,sys,os模块是非常强大的,提供了许多对文件夹.文件和路径的操作方法 sys模块 sys.argv   #命令行执行脚本,其实它就是一个列表 ,sys.argv[0] 是程序自身路 ...

  3. Python学习日记(八)—— 模块一(sys、os、hashlib、random、time、RE)

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  4. 【Python】 sys和os模块

    sys sys模块能使程序访问于python解释器联系紧密的变量和函数 ● sys中的一些函数和变量 argv 命令行参数构成的列表 path 查找所有可用模块所在的目录名的列表 platform 查 ...

  5. sys、os 模块

    sys 模块常见函数 sys.argv           #命令行参数List,第一个元素是程序本身路径 sys.exit(n)        #退出程序,正常退出时exit(0) sys.vers ...

  6. python的sys和os模块

    一.sys sys.argv:实现从程序外部向程序传递参数.  其中sys.argv[0]为脚本的名称,所以要判断是否有参数传入可以:if len(sys.argv) > 1.  sys.exi ...

  7. python笔记6 模块与包 程序开发规范 包 re sys time os模块

    模块与包 python 模块首引用加载到内存,如果再次引用此模块,直接从内存中读取. python文件分为:执行文件(解释器运行的文件),被引用文件(import) 模块引用一共发生了3件事: 1.他 ...

  8. python学习笔记:sys、os模块

    os模块:负责程序与操作系统的交互,提供了访问操作系统底层的接口; sys模块:负责程序与python解释器的交互,提供了一系列的函数和变量,用于操控python的运行时环境. --os 常用方法-- ...

  9. Python中sys和os模块的区别

    sys: This module provides access to some variables used or maintained by the interpreter and to func ...

  10. 【Python】【有趣的模块】【sys&time&os】

    [模块] sys.path.append('C:/Users/wangxue1/PycharmProjects/selenium2TestOne') 然后就可以直接import 这个路径下的模块了 [ ...

随机推荐

  1. js的作用域题

    ---恢复内容开始--- 1. var a = 12 function fn() { console.log(a) var a = 45; console.log(a) } fn() 2. funct ...

  2. Linux下批量修改后缀名

    1.用find和xargs添加后缀名 [root@node99 yum.repos.d]# ls -ltr total 32 -rw-r--r--. 1 root root 5701 Nov 23 2 ...

  3. 简述移动端开发前端和app间的关系

    <p>前端页面嵌套进app内部,一般有时候会进行一些交互,类似于前端页面请求后台接口一样,通常会起一个前端开发人员和app开发人员会相互协定一个协议;双方就协议而言去进行请求接口和返回数据 ...

  4. HttpServlet

    HttpServlet的原理 HttpServlet抽象类中的(部分)方法 HttpServlet extends GenericServlet{ void service(ServletReques ...

  5. Python Flask Restful

    Flask  Restful 1.flask restful 在flask基础上进行一些封装,主要用于实现restful接口 2.restful的理解 1)URI(统一资源标识符):每一个URI代表一 ...

  6. python摸爬滚打之day032 管道 数据共享 进程池

    1.进程池 当有成千上万个任务需要被执行的时候,有了进程池我们就不必去创建大量的进程. 首先,创建进程需要消耗时间,销毁进程(空间,变量,文件信息等等的内容)也需要消耗时间, 第二即便开启了成千上万的 ...

  7. RoR - Restful Actions

    Index: 用于检索所有条目 # index.json.jbuilder json.array!(@post) do |post| json.extract! post, :id, :title, ...

  8. Copy & XCopy

    1):copy不能在有子目录存在的文件中拷贝文件的同时重命名此文件名(注:这里C:为根目录,bat为子目录),而xcopy能,不过会出现提示,当然你可以加参数而不使它提示. C:\>copy c ...

  9. ES6的字符串和数值的扩展

    字符串扩展 对于处理大于两个字节(大于0xffff)的字符,let str =’\u{20bb7}abc’ ES5中的遍历  for(let i=0;i<str.length;i++){ con ...

  10. MySQL 误删数据、误更新数据(update,delete忘加where条件)

    MySQL 误操作后数据恢复(update,delete忘加where条件) 关键词:mysql误删数据,mysql误更新数据 转自:https://www.cnblogs.com/gomysql/p ...