示例代码如下
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)