python输入源文件名和目标文件名,编写拷贝工具,以二进制形式读进来,然后以二进制写入另一个文件

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

示例代码如下

img

import os
source=input('请输入源文件名:')
target=input('请输入目标文件名:')

if os.path.exists(source):
    tpath=os.path.dirname(target)##得到目标文件夹路径
    if not os.path.exists(tpath):#不存在路径则创建
        os.makedirs(tpath)
    r=open(source,'rb')
    w=open(target,'wb')
    while True:
        strb=r.read(1024)
        if strb==b'':
            break
        w.write(strb)
    r.close()
    w.close()
else:
    print('%s不存在'%source)

img