Python 文件操作之Copy

用到的知识点:

类的继承

open(read,write,close)

readline

with open as f 迭代器,自动关闭文件,内存清理

class FileOperation(object):
  def __init__(self):
    print('Start Operate File...')
  
  def Copy(self,src,dst):
    print('srouce file location = ',src)
    print('dest file location = ',dst)
    try:
      file_src = open(src)
      file_dst = open(dst,"w")
      while(True):
        line_src = file_src.readline()
        file_dst.write(line_src)
        if not line_src:
          break
    except:
      print('------------------>Check File if exist!<---------------------')  
    finally:
      file_src.close()
      file_dst.close()
      print('Done File Copy...')

  

class File(FileOperation):
  def __init__(self):
    print('Start WithCopy File...')
  
  def WithCopy(self,src,dst):
    try:
      with open(src,'r') as file_src:
        with open(dst,'w') as file_dst:
          for line in file_src:
            file_dst.write(line)
    except:
      print('WithCopy Failed!')
    finally:
      print('Done WithCopy File!')

if __name__ == '__main__':
  Op_File = File()
  Op_File.Copy('/var/www/Python/FileOp/test.py','/var/www/Python/FileOp/copy.py')
  Op_File.WithCopy('/var/www/Python/FileOp/test.py','/var/www/Python/FileOp/withcopy.py')


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值