本文主要是介绍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:breakexcept: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')
这篇关于Python 文件操作之Copy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!