文章部分来自《笨办法学python》
感谢作者提供这么好的书这一节的主要内容是将一个文件内z容拷贝到另外一个文件里面。
源代码来自《笨办法学python》,注释为我自己添加
#导出系统模块
from sys import argv
from os.path import exists
#给参数赋值
script,from_file,to_file = argv
print "Copying from %s to %s " % (from_file,to_file)
#we could do these two on one line,how?
#打开from_file文件,返回一个文件对象给in_file
in_file = open(from_file)
#读取文件内容以字符串的形式赋给indate
indate = in_file.read()
#返回这个文件大小
print "The input file is %d bytes long" % len(indate)
#返回目标文件是否存在此路径
print "Does the output file exist? %r " % exists(to_file)
#继续脚本按回车,结束按CTRL-C
print "Ready,hit RETURN to continue , CTRL-C to abort."
raw_input()
#用写的方式打开目标文件
out_file = open(to_file,'w')
#将字符写进目标文件
out_file.write(indate)
print "Alright , all done."
in_file.close()
in_file.close()