实现文件内容的拷贝

import os
"""
需求:实现文件内容拷贝
思路:
源文件:读出来
目标文件:写入到
"""
def fileCopy(srcPath,desPath):
if not os.path.exists(srcPath): #判断是否存在
print("哥们,{}文件不存在,别拷贝了".format(srcPath))
return
if not os.path.isfile(srcPath): #是否为文件夹
print("{}不是文件,无法拷贝".format(srcPath))
return
#打卡源文件和目标文件
srcFile = open(srcPath,"rb") #打卡文件 读取内容
desFile = open(desPath,"wb") #打开文件 写入内容

size = os.path.getsize(srcPath) #获取源文件大小,以字节为单位


while size > 0 :

content = srcFile.read(1024) #读取1024字节

desFile.write(content) #写入
size -= 1024

srcFile.close() #关闭文件
desFile.close()
if __name__ == "__main__": #执行程序入库,说白了,程序从这里开始运行

fileCopy("a.txt","c.txt")

你可能感兴趣的:(实现文件内容的拷贝)