写程序,实现复制文件功能......

要求:
     1) 要考虑关闭文件问题
     2) 要考虑超大文件复制问题
     3) 要能复制二进制文件(如:/usr/bin/python3 等文件)

此题需要考虑文件是否可以打开,保证就算打不开程序也不会直接崩溃。

在面对超大文件时是否有一次读取的能力,如果不行计算机内存不够会导致卡顿,所以需要设置缓冲区来进行存储

有些文件并不全是文本文件,在遇到别的数据文件时需要注意功能是否还可以实现

以下为我的Python代码实现

def from_file(filename1,filename2):
    try:
	    a=open(filename1,'rb')
		try:
		    try:
				x=open(filename2,'wb')
				try:
				    a.seek(0,2)
				    c=a.tell()
				    d=0
				    a.seek(0,0)
				    while True:
					if c>4096:
					    b= a.read(4096)
					else:
					    b=a.read()
					    print(b)
					    x.write(b)
					    x.flush()
					    break
					c=a.tell()
					print(b)
					x.write(b)
					x.flush()
					d+=1
				    a.close()
				    x.close()
				finally:
					x.close()
			except OSError:
				print("打开目标文件失败")
		finally:	
			a.close()
	except OSError:
		print("打开源文件失败")

 下面是进行输入,可以实现不同目录下的复制

n=input()
m=input()
from_file(n,m)

 

 



相关文章:

  • 2022-12-23
  • 2021-08-24
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-03-05
  • 2022-12-23
  • 2021-05-15
  • 2022-12-23
  • 2021-10-25
相关资源
相似解决方案