LPTHW L16 NOTE

来源:互联网 发布:可可验证源码 编辑:程序博客网 时间:2024/06/05 19:39
# -*- coding: UTF-8 -*-from sys import argvscript, file_name = argvprint ("We are going to erase %r." % file_name)# ctrl-c可以用来退出正在执行中的脚本print ("If you do not want that, hit CRTL -C (^C).")print ("If you do want that, hit RETURN.")input("?")print ("Now, let's opening the file...")'''这里使用了open方法,open的参数有如下几种:w     以写方式打开,a     以追加模式打开 (从 EOF 开始, 必要时创建新文件)r+     以读写模式打开w+     以读写模式打开 (参见 w )a+     以读写模式打开 (参见 a )"+"修饰符,这样的话文件将以同时读写的方式打开'''target = open(file_name, 'w+')print ("Now, we gonna truncating the file. Good bye")target.truncate()# 这里让用户可以输入三个句子print ("Now I gonna ask you for three lines.")line1 = input("line 1: ")line2 = input("line 2: ")line3 = input("line 3: ")# 这里开始向文件写入这三个句子 print ("I'm going to write these to the file.")'''较冗杂的写入方式target.write(line1)  target.write("\n")  target.write(line2)  target.write("\n")  target.write(line3)  target.write("\n") '''# 这里通过格式化字符来简化代码target.write("%s\n%s\n%s\n" % (line1, line2, line3))print ("And finally, we close it.")target.close()

0 0