python实现复制文件功能

  1. 写程序,实现复制文件功能
    要求:
    1) 要考虑关闭文件问题
    2) 要考虑超大文件复制问题
    3) 要能复制二进制文件(如: / usr / bin / python3 等文件)
    def copy_file():
    file=input(‘源文件’)
    file1=input(‘复制文件’)
    try:

    offset=0
    while True:
    
        f=open(file, 'rb')
        if offset > 0:
            f.seek(offset, 0)
        l=f.read(4096)
        f.close()
        offset += 4096
        print(l)
        if len(l) == 0:
            break
        else:
    
            f1=open(file1, 'wb')
            f1.seek(offset - 4096, 0)
            f1.write(l)
            f1.close()
    

    except OSError:
    print(‘文件出错’)
    copy_file()

猜你喜欢

转载自blog.csdn.net/weixin_32759777/article/details/82120099