小文件复制:
sourceFileName = input("请输入你要复制的文件名:")
index = sourceFileName.rfind(".") # 除用字符串的内置函数外,还可以使用条件语句判断
copyFileName = sourceFileName[:index]+ "-副本"+sourceFileName[index:]
readFile = open(sourceFileName,"rb")
content = readFile.read()
writeFile = open(copyFileName,"wb")
writeFile.write(content)
print("文件复制完成...")
readFile.flush()
writeFile.flush()
readFile.close()
writeFile.close()
大文件复制:
sourceFileName = input("请输入你要复制的文件名:")
listStr = sourceFileName.split(".")
copyFileName = listStr[0] +"-副本."+listStr[1] # 以防文件命名格式不正确做出的改变
readFile = open(sourceFileName,"rb")
writeFile = open(copyFileName,"wb")
while True:
content = sourceFileName.read(1024*1024) #read()的单位为byte,让其以1MB传输
if len(content)==0:
break
writeFile.write(content)
writeFile.flush()
print("文件复制完成...")
readFile.close()
writeFile.close()
欢迎各位补充和纠错