3 案例3:模拟cp操作
3.1 问题
创建cp.py文件,实现以下目标:
将/bin/ls“拷贝”到/root/目录下
不要修改原始文件
- 1
- 2
3.2 方案
获取用户原文件名和新文件名,打开原文件,打开新文件,从打开的原文件中读取数据,写入到打开的新文件中,关闭两个文件
cp代码的过程中,需要注意的部分在于:
如果一个文件过大,你将无法直接读取数据到内存,此时,使用while循环语句,分次读取数据,每次读4096字节,读取数据为空时,结束循环,将数据写入到目标文件
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:编写脚本
[root@localhost day03]# vim cp.py
#!/usr/bin/env python3
f1 = open('/bin/ls', 'rb')
f2 = open('/root/ls', 'wb')
data = f1.read()
f2.write(data)
f1.close()
f2.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
或将上面的代码改为以下写法:
循环读取文件中数据,避免读取数据过大
[root@localhost day03]# vim cp2.py
#!/usr/bin/env python3
src_fname = '/bin/ls'
dst_fname = '/tmp/ls'
src_fobj = open(src_fname, 'rb')
dst_fobj = open(dst_fname, 'wb')
while True:
data = src_fobj.read(4096) # 每次读4096字节
if data == b'': # 读不到数据意味着读写完毕,中断循环
break
dst_fobj.write(data) # 将数据写到目标文件
src_fobj.close()
dst_fobj.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
或将上面的代码改为以下写法:
With打开文件读取数据或写入数据后,文件会直接关闭
[root@localhost day03]# vim cp3.py
#!/usr/bin/env python3
src_fname = '/bin/ls'
dst_fname = '/root/ls'
with open(src_fname, 'rb') as src_fobj:
with open(dst_fname, 'wb') as dst_fobj:
while True:
data = src_fobj.read(4096)
if not data:
break
dst_fobj.write(data)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
或将上面的代码改为以下写法:
sys.argv方法表示空列表,执行脚本时输入命令: python3 cp_func.py /bin/ls /root/ls,表示sys.argv=[cp_func.py,‘/bin/ls’,‘/root/ls’],所以,调用copy函数时,列表切片方式获取实参为(‘/bin/ls’,‘/root/ls’)
[root@localhost day03]# vim cp_func.py
#!/usr/bin/env python3
import sys
def copy(src_fname, dst_fname):
src_fobj = open(src_fname, 'rb')
dst_fobj = open(dst_fname, 'wb')
while True:
data = src_fobj.read(4096)
if not data:
break
dst_fobj.write(data)
src_fobj.close()
dst_fobj.close()
copy(sys.argv[1], sys.argv[2])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
步骤二:测试脚本执行
[root@localhost day03]# python3 cp.py
[root@localhost day03]# cd /root
[root@localhost ~]# ls
core ls
[root@localhost day03]# python3 cp2.py
[root@localhost day03]# cd /root
[root@localhost ~]# ls
core ls
[root@localhost day03]# python3 cp3.py
[root@localhost day03]# cd /root
[root@localhost ~]# ls
core ls
[root@localhost day03]# python3 cp_func.py /bin/ls /root/ls
[root@localhost day03]# cd /root
[root@localhost ~]# ls
core ls
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了404 篇原创文章</span> · <span>获赞 56</span> · <span>访问量 4万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm attented bt-button personal-watch" data-report-click="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>