IT虾米网

Python文件拷贝

xmjava 2018年06月24日 编程语言 1256 0

[Python]代码    

#coding:cp936 
''' 
Created on 2011-6-21 
 
@author: tangly 
文件拷贝 
''' 
import  os 
 
def  MyCopyFile(srcfile, destfile): 
	if  not os.path.exists(srcfile): 
		print "%s这个文件不存在或你的文件路径出现错误!" % srcfile 
	if not os.path.exists(destfile): 
		print "%s这个文件不存在或你的文件路径出现错误!" % destfile 
		print "请问是否要重新建立这个文件?(y||n)" 
		if raw_input() == 'y' or raw_input() == 'Y': 
			 f = open(destfile, 'w')  #建立文件 
		else: 
			print "文件拷贝创建失败!" 
		f.close() 
	 
	f1 = open(srcfile, 'r') 
	f2 = open(destfile, 'w') 
	while 1: 
		text = f1.read(50) 
		if text == "": 
			break 
		f2.write(text) 
	f1.close() 
	f2.close() 
	return  
 
src = raw_input("请输入被拷贝的源文件:\n") 
dest = raw_input("请输入拷贝到那个文件:\n") 
MyCopyFile(src, dest) 
评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!