1 # -*- coding:utf-8 -*- 2 3 from sys import argv 4 5 script, filename = argv #解包变量参数 6 7 print ("we‘re going to erase %r." % filename) 8 print ("if you don‘t want that, hit CTRL-C(^c).") #CTRL-C的用法 9 print ("if you do want that, hit RETURN.") # RETURN的用法 10 11 input ("?") 12 13 print ("opening the file...") 14 target = open(filename, ‘w‘) #‘w‘的意识是‘write’,不可写成大写,否则出现语法错误。本句为定义变量target 15 16 # 学会该如何打开一个其它文件目录的文件????? 17 #‘r‘ open for reading (default) 18 #‘w‘ open for writing, truncating the file first ‘x‘ create a new file and open it for writing 19 #‘a‘ open for writing, appending to the end of the file if it exists 20 #‘b‘ binary mode 二进制 21 #‘t‘ text mode (default) 文本模式 22 #‘+‘ open a disk file for updating (reading and writing) 23 #‘U‘ universal newline mode (deprecated) 24 25 print ("truncating the file .Goodbye!") 26 #target.truncate() #truncate()的意思是清空文件 27 28 print ("Now I‘m going to ask you for three lines.") 29 30 line1 = input ("line 1: ") 31 line2 = input ("line 2: ") 32 line3 = input ("line 3: ") 33 34 print ("I‘m going to write these to the file.") 35 36 target.write(line1) #write()的意思是写入文件 37 target.write("\n") 38 target.write(line2) 39 target.write("\n") 40 target.write(line3) 41 target.write("\n") 42 43 print ("And finally,we close it.") 44 target.close() # 关闭文件和保存的意思
时间: 2024-10-06 16:20:06