笨方法学Python3 习题16巩固练习

我的习题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() #关闭文件,这样就把文件写入磁盘里头,而不是在内存里缓冲

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值