我的习题16的一些详细的注释:
from sys import argv #载入argv模块,获取脚本运行参数
script, filename = argv #解包argv,并将脚本名赋值给script;将参数赋值给变量filename
print(f"We are going to erase {filename}.")
print("If you don't want that, hit CTRL-C (^C).") #KeyboardInterrupt键盘中断
print("If you do want that, hit RETURN.")
input("?") #让用户选择
print("open the file...")
target = open(filename, 'w') #Open文件,the mode is 'w',只写模式,清空内容然后新建,不可以read,赋值给fileobject,the fileobject
#print(target.name()) #TypeError: 'str' object is not callable
#因为模式是w,所以不可read,不可读出name
print(target.tell()) #tell()是指出当前光标位置,然后打印出来
print("Truncating the file. Goodbye!")
target.truncate() #没有指定 size,则从当前位置起截断
print("Now I'm going to ask you for three lines.")
#input输入三行字符串
line1 = input("line1: ")
line2 = input("line2: ")
line3 = input("line3: ")
print(target.tell()) #tell()是指出当前光标位置,然后打印出来
print("I'm going to write these to the file.")
#target.write(line1) #把输入的line1 write写入进去文件
#target.write('\n')
#target.write(line2)
#target.write('\n')
#target.write(line3)
#target.write('\n')
#此处想要简化上述write命令
#这是一种方式
#tw = f"{line1}\n{line2}\n{line3}\n"
#target.write(tw)
#target.write(f"{line1}\n{line2}\n{line3}\n")
#这是另一种方式
#tw = "{}\n{}\n{}\n"
#target.write(tw.format(line1,line2,line3))
target.write("{}\n{}\n{}\n".format(line1,line2,line3))
#这是第三种方式
#target.write("%s\n%s\n%s\n"%(line1,line2,line3))
#此时读写位置在文件末尾
#truncate()用法是在光标读写位置截断,现在在末尾,在此处截断,和不截断一样
target.seek(0) #这里是将光标位置seek到文件开头
#光标移到文件开头,还可以先close,然后再open,这是另一种方法
#target.truncate() #然后在此处截断,就把后面的都删掉了,最后用>>>more test.txt 啥也灭有
print("And finally, we close it.")
target.close() #关闭文件,这样就把文件写入磁盘里头,而不是在内存里缓冲
这是代码txt格式,看的更详细一点儿:
from sys import argv #载入argv模块,获取脚本运行参数
script, filename = argv #解包argv,并将脚本名赋值给script;将参数赋值给变量filename
print(f"We are going to erase {filename}.")
print(“If you don’t want that, hit CTRL-C (^C).”) #KeyboardInterrupt键盘中断
print(“If you do want that, hit RETURN.”)
input("?") #让用户选择
print(“open the file…”)
target = open(filename, ‘w’) #Open文件,the mode is ‘w’,只写模式,清空内容然后新建,不可以read,赋值给fileobject,the fileobject
#print(target.name()) #TypeError: ‘str’ object is not callable
#因为模式是w,所以不可read,不可读出name
print(target.tell()) #tell()是指出当前光标位置,然后打印出来
print(“Truncating the file. Goodbye!”)
target.truncate() #没有指定 size,则从当前位置起截断
print(“Now I’m going to ask you for three lines.”)
#input输入三行字符串
line1 = input("line1: ")
line2 = input("line2: ")
line3 = input("line3: ")
print(target.tell()) #tell()是指出当前光标位置,然后打印出来
print(“I’m going to write these to the file.”)
#target.write(line1) #把输入的line1 write写入进去文件
#target.write(’\n’)
#target.write(line2)
#target.write(’\n’)
#target.write(line3)
#target.write(’\n’)
#此处想要简化上述write命令
#这是一种方式
#tw = f"{line1}\n{line2}\n{line3}\n"
#target.write(tw)
#target.write(f"{line1}\n{line2}\n{line3}\n")
#这是另一种方式
#tw = “{}\n{}\n{}\n”
#target.write(tw.format(line1,line2,line3))
target.write("{}\n{}\n{}\n".format(line1,line2,line3))
#这是第三种方式
#target.write("%s\n%s\n%s\n"%(line1,line2,line3))
#此时读写位置在文件末尾
#truncate()用法是在光标读写位置截断,现在在末尾,在此处截断,和不截断一样
target.seek(0) #这里是将光标位置seek到文件开头
#光标移到文件开头,还可以先close,然后再open,这是另一种方法
#target.truncate() #然后在此处截断,就把后面的都删掉了,最后用>>>more test.txt 啥也灭有
print(“And finally, we close it.”)
target.close() #关闭文件,这样就把文件写入磁盘里头,而不是在内存里缓冲