通过上一个习题的学习,了解了“对象.方法”的运用还有文件读取的方法。这个一个习题学习的是文件的写入。
首先放上代码和解释:
#-*-coding:utf-8-*-
from sys import argv #从sys模块中导入argv对象
script , filename = argv #将从terminal中输入的参数赋值给变量
print"We're going to erase %r" %filename #打印提示信息
print"If you don't want that , hit CRTL-C(^C)" #打印提示信息,CRTL-C为终端执行指令
print"If you do want that , hit RETRUN" #打印提示信息,RETURN相当于输入空值,程序继续执行
raw_input("?") #输入指令
print"Open the file..." #提示信息:正在打开文件
target = open(filename , 'w') #以写入模式打开filename文件,返回的文件对象赋值给target
print"Truncating the file. Goodbye!" #提示输入:正在清空文件
target.truncate() #target的方法:truncate清空
print"Now I‘m going to ask you for three lin