文件io操作作业练习

实现copy函数:

1、指定一个源文件,实现copy到目标目录

例如把/tmp/test.txt拷贝到/tmp/test1.txt  

from os import path

base = '/tmp'
src = 'test.txt'
dst = 'test1.txt'

src = path.join(base,src)
dst = path.join(base,dst)

print(src,dst)


with open(src,'w',encoding='utf-8') as f:
    f.write('\n'.join(['abc','123','alren']))


def copy(src,dst):
    with open(src,'rb') as f1:
        with open(dst,'wb') as f2:
            length = 16 * 1024
            while True:
                buffer = f1.read(length)
                if not buffer:
                    break
                f2.write(buffer)


copy(src,dst)

#使用内嵌函数实现

import shutil

shutil.copy2(src,dst)

 

posted @ 2020-05-10 22:18  Alrenn  阅读(162)  评论(0)    收藏  举报