learn python the hard way: ex 16

文章目录

ex16

  • close – 关闭文件。跟你编辑器的 文件->保存… 一个意思。
  • read – 读取文件内容。你可以把结果赋给一个变量。
  • readline – 读取文本文件中的一行。
  • write(‘stuff’) – 将stuff写入文件。
    -truncate – 清空文件,请谨慎使用该命令。

code

from sys import argv  # 引入系统的部分模块参数变量
script, filename = argv, "filename"  # 将参数解包并赋值给两个变量,再手动输入一个参数,否则会报错

print(f"We're going to erase {filename}.")  # 抹去,消除。将变量嵌入字符串并打印
print("If you don't want that, hit CTRL-C(^C).")  # 打印字符串
print("If you do want that, hit RETURN.")  # 打印字符串

input("?")  # 输入提示信息,接收输入内容

print("Opening the file...")
target = open(filename, 'w')  # 目标。用open函数以写入模式打开变量,并将文件放入target

print("Truncating the file. Goodbye!")  # 缩短,清空
target.truncate()  # 在变量target后面调用truncate函数(清空文件

print("Now I'm going to ask you for three lines.")

line1 = input("line1:")  # 输入提示信息,接收输入内容并存入变量line1
line2 = input("line2:")
line3 = input("line3:")

print("I'm going to write these to the file.")

target.write(line1)  # 在变量target后面调用write函数,写入变量line1中的内容
target.write("\n")  # ....作用换行
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print("And finally, we close it.")
target.close()  # 在变量target后面调用close函数,关闭文件

result

We're going to erase filename.
If you don't want that, hit CTRL-C(^C).
If you do want that, hit RETURN.
?^C
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines.
line1:I want to go back home.
line2:But I have to run 4 miles first.
line3:Then 1.5 hours later I will see my lover.
I'm going to write these to the file.
And finally, we close it.
We're going to erase filename.
If you don't want that, hit CTRL-C(^C).
If you do want that, hit RETURN.
?RETURN
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines.
line1:What is the meaning of the question and hit?
line2:I can't see any difference.
line3:So I have to ask my boyfriend----a genius in my heart.
I'm going to write these to the file.
And finally, we close it.

#Q2.写一个和上一个练习类似的脚本,使用 read 和 argv 读取你刚才新建的文件。
code

from sys import argv
script, filename = argv, "test.txt"

txt = open(filename)

print("Let's read the file.")
print("\n")
print(txt.read())

txt.close()

result
(错误原因:read后没有加())

Let's read the file.


What is the meaning of the question and hit?
I can't see any difference.
So I have to ask my boyfriend----a genius in my heart.

Q3.这个文件中重复的地方太多了。试着用一个target.write()将line1,line2,line3打印出来,替换掉原来的6行代码。你可以使用字符串、格式化字符、和转义字符。

这里直接用加号连接两个字符串:

from sys import argv  # 引入系统的部分模块参数变量
script, filename = argv, "filename"  # 将参数解包并赋值给两个变量,再手动输入一个参数,否则会报错

print(f"We're going to erase {filename}.")  # 抹去,消除。将变量嵌入字符串并打印
print("If you don't want that, hit CTRL-C(^C).")  # 打印字符串
print("If you do want that, hit RETURN.")  # 打印字符串

input("?")  # 输入提示信息,接收输入内容

print("Opening the file...")
target = open(filename, 'w')  # 目标。用open函数以写入模式打开变量,并将文件放入target

print("Truncating the file. Goodbye!")  # 缩短,清空
target.truncate()  # 在变量target后面调用truncate函数(清空文件

print("Now I'm going to ask you for three lines.")

line1 = input("line1:")  # 输入提示信息,接收输入内容并存入变量line1
line2 = input("line2:")
line3 = input("line3:")

print("I'm going to write these to the file.")

target.write(line1 + '\n' + line2 + '\n' + line3 + '\n')  # 在变量target后面调用write函数,写入变量line1中的内容

print("And finally, we close it.")
target.close()  # 在变量target后面调用close函数,关闭文件

result

We're going to erase filename.
If you don't want that, hit CTRL-C(^C).
If you do want that, hit RETURN.
?RETURN
Opening the file...
Truncating the file. Goodbye!
Now I'm going to ask you for three lines.
line1:I love Taylor Swift
line2:Her songs are therapeutic.
line3:The new album called lover is coming.
I'm going to write these to the file.
And finally, we close it.

question:
target write有没有都行?

参数调用改成test.txt,加入target.write会把line123写入test.txt(放在和ex16同一个目录就行)
在这里插入图片描述

Q4.找出为什么我们需要给open多赋予一个’w’参数。提示:open对于文件的写入操作态度是安全第一,所以你只有特别指定以后它才会进行写入操作。
open() 的默认参数是open(file,‘r’) 也就是读取文本的模式,默认参数可以不用填写。而本题练习是写入文件,因此不适应使用r参数,需要指定写入模式,因此需要用w参数。

Q5.如果你用写入模式打开文件,那么你是不是还需要target.truncate()呢?阅读一下python的open函数的文档找找答案。
Python的open函数文档中说:“It will be truncated when opened for writing.”,也就是说truncate()不是必须的。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值