#此示例with语句用法
src_file = input("请输入源文件")
try:
src = open(src_file,'rb')
#准备打开另一个文件
#开始读文件
#关闭文件
try:
try:
dst_file = input("请输入目标文件")
dst = open(dst_file)
try:
b = src.read(4096)
dst.write(b,'wb')
finally:
dst_file.close()
except OSError:
print("打开写文件失败")
finally:
src.close()
except OSError:
print("打开文件失败")
#---------------------------------------------------
src_file = input("请输入源文件")
try:
src = open(src_file,'rb')
#准备打开另一个文件
#开始读文件
#关闭文件
try:
try:
dst_file = input("请输入目标文件")
with open(dst_file ,"wb") as dst:
b = src.read()
dst.write(b,'wb')
except OSError:
print("打开写文件失败")
finally:
src.close()
except OSError:
print("打开文件失败")
#------------------------------------
src_file = input("请输入源文件")
try:
with open(src_file,'rb') as src:
#准备打开另一个文件
#开始读文件
#关闭文件
try:
dst_file = input("请输入目标文件")
with open(dst_file ,"wb") as dst:
b = src.read()
dst.write(b,'wb')
except OSError:
print("打开写文件失败")
except OSError:
print("打开文件失败")
#---------------------------------------
src_file = input("请输入源文件")
try:
with open(src_file,'rb') as src:
#准备打开另一个文件
#开始读文件
#关闭文件
dst_file = input("请输入目标文件")
with open(dst_file ,"wb") as dst:
b = src.read()
dst.write(b,'wb')
except OSError:
print("复制失败")
#------------------------------------------
src_file = input("请输入源文件")
dst_file = input("请输入目标文件")
try:
with open(src_file,'rb') as src,open(dst_file ,"wb") as dst:
b = src.read()
dst.write(b,'wb')
except OSError:
print("复制失败")