您的位置:首页 > 编程语言 > Python开发

笨办法学Python学习笔记 练习17

2016-01-12 17:13 585 查看
from sys import argv
from os.path import exists
#通过argv函数,给from_file,to_file赋值
script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

#we could do these two on one line too, how?
input = open(from_file) #用只读的方式打开文件
indata = input.read() #将文件的内容赋值到indata中

print "The input file is %d bytes long" % len(indata)#得到文件的长度

print "Does the out put file exist? %r" % exists(to_file)#判断to_file文件是否存在
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

output = open(to_file,'w')#用读写方模式打开文档
output.write(indata)#将indata的内容写入到to_file文档中

print "Alright, all done."
#保存
output.close()
input.close()




运行结果



test.txt文件内容,运行程序之后建立了copied.txt文件,并且将内容复制过来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 学习笔记