exercise 16

from sys import argv

script, filename = argv

print(f"We're going to erase {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.")

# 這個(gè)input()僅僅是等待輸入,按任意鍵輸入就會(huì)運(yùn)行程序,按ctrl-c就會(huì)終止運(yùn)行
input("?")

# 打印
print("Opening the file...")
# 在文件中寫(xiě)入
target = open(filename, 'w')

# 打印
print("Truncating the file. Goodbye!")
# 清除文件內(nèi)容
target.truncate()

# 打印
print("Now I'm going to 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.")

# 文件中寫(xiě)入第三行
target.write(line1)
#文件中換行
target.write("\n")
# 文件中寫(xiě)入第三行
target.write(line2)
# 文件中換行
target.write("\n")
# 文件寫(xiě)入第三行
target.write(line3)
# 寫(xiě)入,換行
target.write("\n")

# 打印
print("And finally, we close it.")
# 關(guān)閉文件
target.close()

練習(xí)

  1. If you do not understand this, go back through and use the comment trick to get it squared away inyour mind. One simple English comment above each line will help you understand or at least let youknow what you need to research more.
  2. Write a script similar to the last exercise that uses read and argv to read the file you just created.
  3. There’s too much repetition in this file. Use strings, formats, and escapes to print out line1 ,line2 , and line3 with just one target.write() command instead of six.
  4. Find out why we had to pass a 'w' as an extra parameter to open . Hint: open tries to be safe by making you explicitly say you want to write a file.
  5. If you open the file with 'w' mode, then do you really need the target.truncate() ? Read thedocumentation for Python’s open function and see if that’s true.

答案

from sys import argv
script,filename = argv

txt = open(filename,'r')
print(txt.read())
from sys import argv

script, filename = argv

print(f"We're going to erase {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.")

# 這個(gè)input()僅僅是等待輸入,按任意鍵輸入就會(huì)運(yùn)行程序,按ctrl-c就會(huì)終止運(yùn)行
input("?")

# 打印
print("Opening the file...")
# 在文件中寫(xiě)入
target = open(filename, 'w')

# 打印
print("Truncating the file. Goodbye!")
# 清除文件內(nèi)容
target.truncate()

# 打印
print("Now I'm going to 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.")

# 文件中寫(xiě)入第三行
target.write(f"{line1}\n{line2}\n{line3}\n")

# 打印
print("And finally, we close it.")
# 關(guān)閉文件
target.close()

5.沒(méi)必要用target.truncate(),用為read模式會(huì)覆蓋原文件。

Q: 'w'參數(shù)是什么意思?

它只是打開(kāi)文件的一種模式。如果你用了這個(gè)參數(shù),表示"以寫(xiě)(write)模式打開(kāi)文件。同樣有'r'表示只讀模式,'a'表示追加模式,還有一些其他的修飾符。
Q: 有什么修飾符我可以用在打開(kāi)文件的模式上?

最重要的一個(gè)就是+, 使用它,你可以有'w+', 'r+', 和'a+'模式. 這樣可以同時(shí)以讀寫(xiě)模式打開(kāi)文件。
Q: open(filename)是以'r' (只讀) 模式打開(kāi)文件嗎?

是的,'r' 是 open()函數(shù)的默認(rèn)參數(shù)值。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容