python-重写cp指令

python代码写cp命令

简化版(不推荐,但可运行)

f1 = open("/bin/ls","rb")
f2 = open("/tmp/ls","wb")
data = f1.read()
f2.write(data)
f1.close()
f2.close()

提升版

src_fname = '/bin/ls'
dst_fname = '/tmp/ls'
src_fobj = open(src_fname,'rb')
dst_fobj = open(dst_fname,'wb')
while 1 :
	data = src_fobj.read(4096)
	if not data:
		break
	dst_fobj.write(data)
src_fobj.close()
dst_fobj.close()

函数版

新建mycopy.py文件

import sys
def mycopy(src_fname,dst_fname)
	# src_fname = '/bin/ls'
	# dst_fname = /tmp/ls'
	src_fobj = open(src_fname,'rb')
	dst_fobj = open(dst_fname,'wb')
	while 1 :
		data = src_fobj.read(4096)
		if not data:
			break
		dst_fobj.write(data)
	src_fobj.close()
	dst_fobj.close()
mycopy(sys.argv[1],sys.argv[2])
# execute
# python3 mycopy.py /bin/ls /tmp/ls

你可能感兴趣的:(python-重写cp指令)