Python基础之shutil模块、random模块
1.shutil模块
shutil模块是对os模块的功能补充,包含移动、复制、打包、压缩、解压等功能。
1)shutil.copyfileobj()
复制文件内容到另一个文件,可指定大小内容,如length=16*1024。
import shutil
f_old = open('old_file','r')
f_new = open('new_file','w')
shutil.copyfileobj(f_old,f_new,length=*)
2)shutil.copyfile()
同样将文件内容复制到另一个文件,但是可以直接使用文件名,无须用open()方法打开,事实上此方法调用了shutil.copyfileobj()方法。
import shutil
shutil.copyfile('f_old','f_new')
3)shutil.copymode()
每个文件创建时要赋予其一定的权限,比如,只读、可读可写等等,shutil.copymode()方法是将文件的权限复制给另一个文件。
import shutil
shutil.copymode('f_old','f_new')
4)shutil.copystat()
将文件所有的状态信息复制至另一个文件,包括权限,组,用户,时间等。
import shutil
shutil.copystat('f_old','f_new')
5)shutil.copy()
将文件的内容和权限复制给另一个文件,相当于先用shutil.copyfile()方法,再用shutil.copymode()方法。
import shutil
shutil.copy('f_old','f_new')
6)shutil.copy2()
将文件内容和所有信息状态复制给另一个文件,相当于先用shutil.copyfile()方法,再用shutil.copystat()方法。
import shutil
shutil.copy2('f_old','f_new')
7)shutil.copytree()
可以递归复制多个目录到指定目录下,shutil.copytree(源目录,目标目录,True/False)。
8)shutil.rmtree()
递归的删除一个目录和目录下的所有文件。
9)shutil.move()
递归的移动文件,相当于重命名。
10)shutil.make_archive()
shutil.make_archive(base_name,format,root_dir),压缩,打包文件。
base_name:压缩包的文件名,也可以是路径,如果是文件名时保存到当前目录,如果是路径时保存至指定路径。
format:压缩包种类,zip/tar/bztar/gztar。
root_dir:要压缩的文件夹路径。
将 F:\\python\\week4 路径下的文件打包放到 当前目录 下,压缩包名字为f_old,压缩方式为zip。
import shutil
ret = shutil.make_archive("f_old", 'zip', root_dir='F:\\python\\week4')
将 F:\\python\\week4 路径下的文件打包放到 F:\\python\\week2 路径下,压缩包名字为f_old,压缩方式为zip。
import shutil
ret = shutil.make_archive("F:\\python\\week2\\f_old", 'zip', root_dir='F:\\python\\week4')
11)ZipFile和TarFile模块
shutil对压缩包的处理是调用ZipFile和TarFile模块。
2.random模块
random模块用于生成随机数。
1)random.random()
random.random()用于生成一个0.0-1.0的随机浮点数。
import random
print(random.random())
结果:
0.7381125452476276
2)random.uniform()
random.uniform(a,b)用于生成一个指定上下限的随机浮点数,包括上下限值。
import random
print(random.uniform(,))
结果:
48.088142353356744
3)random.randint()
random.randint(a,b)用于生成一个指定上下限的随机整型数,包括上下限。
import random
print(random.randint(,))
结果:
4)random.randrange()
random.randrange()用于生成一个指定上下限的随机整型数,包括左边不包括右边。
import random
print(random.randrange(,))
结果:
5)random.choice()
random.choice(),从中随机选取一个值。
import random
print(random.choice([,,,,,]))
结果:
6)random.shuffle()
rando.shuffle()将一个列表中的元素打乱。
import random
lis = [,,,,,]
random.shuffle(lis)
print(lis)
结果:
[, , , , , ]
7)random.sample()
random.sample()从指定序列中随机选取指定长度的字符。
import random
ret = random.sample('abcdefg',)
print(ret)
结果:
['a', 'c', 'f']
Python基础之shutil模块、random模块的更多相关文章
- Day13 Python基础之time/datetime/random模块一(十一)
time模块 import time print(help(time)) time.time() #return current time in seconds since the Epoch as ...
- python常用模块——random模块
参考博客:http://www.360doc.com/content/14/0430/11/16044571_373443266.shtml 今天突然想起python该怎么生成随机数?查了一下,贴出实 ...
- Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)
Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...
- Python模块01/自定义模块/time模块/datetime模块/random模块
Python模块01/自定义模块/time模块/datetime模块/random模块 内容大纲 1.自定义模块 2.time模块 3.datetime模块 4.random模块 1.自定义模块 1. ...
- python shutil模块&random模块
shutil模块 import shutil shutil.copyfileobj(open("os_.py", "r"), open("os_2.p ...
- Py修行路 python基础 (二十)模块 time模块,random模块,hashlib模块,OS及sys模块
一.前提介绍: 可以开辟作用域的只有类,函数,和模块 for循环 if,else: 不能开辟自己的作用域 避免程序复用和重复调用,将这些写到一个.py文件中,做成一个模块,进行调 ...
- python 全栈开发,Day27(复习, defaultdict,Counter,时间模块,random模块,sys模块)
一.复习 看下面一段代码,假如运行结果有问题,那么就需要在每一步计算时,打印一下结果 b = 1 c = 2 d = 3 a = b+c print(a) e = a + d print(e) 执行输 ...
- python time模块 sys模块 collections模块 random模块 os模块 序列化 datetime模块
一 collections模块 collections模块在内置的数据类型,比如:int.str.list.dict等基础之上额外提供了几种数据类型. 参考博客 http://www.pythoner ...
- Python:time模块/random模块/os模块/sys模块
time 模块 #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time() 获取当前时间戳 python中时间日期格式化符号: %y 两位数的 ...
随机推荐
- Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)
原文:https://blog.csdn.net/hubo_88/article/details/80671192 Spring Boot Admin 用于监控基于 Spring Boot 的应用,它 ...
- Jupyter notebook 添加或删除内核
1.切换到要添加的虚拟环境,确认是否安装 ipykernel python -m ipykernel --version 如果没有安装,则安装: python -m pip install ipyke ...
- 数据结构篇——平衡二叉树(AVL树)
引入 上一篇写了二叉排序树,构建一个二叉排序树,如果构建序列是完全有序的,则会出现这样的情况: 显然这种情况会使得二叉搜索树退化成链表.当出现这样的情况,二叉排序树的查找也就退化成了线性查找,所以我们 ...
- hdu2281&&POJ1320——Pell方程
hdu2281 输入一个 $N$,求最大的 $n$($n \leq N$)和 $x$,使得 $x^2 = \frac{1^2+2^2+...+n^2}{n}$. 分析: 将右边式子的分子求和化简,有: ...
- SQL Server 默认跟踪(Trace)捕获事件详解
SQL Server 默认跟踪 -- 捕获事件详解 哪些具体事件默认跟踪文件能够捕获到? --returns full list of events SELECT * FROM sys.trace_e ...
- ABP 往前端返回详细的错误信息
在这个类:MyABP.Web.Startup.MyABPWebMvcModule 中 的 PreInitialize 方法 添加一句: Configuration.Modules.AbpWebComm ...
- 洛谷 P1234 小A的口头禅
这里是传送门啊 I'm here! 题目描述 小A最近有了一个口头禅"呵呵",于是他给出了一个矩形,让你求出里面有几个hehe(方向无所谓). 输入输出格式 输入格式: 第一行两个 ...
- JavaScriptDOM编程学习笔记(二)图片库案例
<JavascriptDOM编程艺术>提供一个图片库的demo,主要讲解如何更好的使用JavaScript在网页中,跟随作者的思路来分析一下这个案例 首先需求是将图片发布到网上,但是如果发 ...
- C# Process.Start()函数打开url被360拦截问题
使用Process.Start(new ProcessStartInfo(url))来打开某一网址的时候,往往会被360提示 类似这样的 信息: “威胁:修改此注册表项将更改IE连接设置.少数软件会修 ...
- 【2019年05月07日】A股最便宜的股票
新钢股份(SH600782) - 当前便宜指数:193.2 - 滚动扣非市盈率PE:2.99 - 滚动市净率PB:0.87 - 动态年化股息收益率:1.68%- 新钢股份(SH600782)的历史市盈 ...