《笨辦法學(xué)Python3》練習(xí)十六:讀寫文件

練習(xí)代碼

from sys import argv

script, filename = argv

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')

print("Truncating the file.  Goodbye!")
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.")

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()

Study Drills

  1. If you do not understand this, go back through and use the comment trick to get it squared away in your mind. One simple English comment above each line will help you understand or at least let you know 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.

target.write(line1+"\n"+line2+"\n"+line3+"\n")
  1. 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.

open()方法的默認(rèn)模式是r帝火,也就是只讀模式村生,無(wú)法寫入。

  1. If you open the file with 'w' mode, then do you really need the target.truncate()? Read
    the documentation for Python’s open function and see if that’s true.

不是必需的灶轰。w模式會(huì)在寫入前自動(dòng)truncate
詳見(jiàn) 官方文檔

補(bǔ)充

  1. open的常用模式
    chrome_rMhjam4m4c.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末噪奄,一起剝皮案震驚了整個(gè)濱河市死姚,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌勤篮,老刑警劉巖都毒,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異碰缔,居然都是意外死亡账劲,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門金抡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)瀑焦,“玉大人,你說(shuō)我怎么就攤上這事梗肝¢晃停” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵巫击,是天一觀的道長(zhǎng)禀晓。 經(jīng)常有香客問(wèn)我精续,道長(zhǎng),這世上最難降的妖魔是什么匆绣? 我笑而不...
    開(kāi)封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任驻右,我火速辦了婚禮,結(jié)果婚禮上崎淳,老公的妹妹穿的比我還像新娘堪夭。我一直安慰自己,他們只是感情好拣凹,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開(kāi)白布森爽。 她就那樣靜靜地躺著,像睡著了一般嚣镜。 火紅的嫁衣襯著肌膚如雪爬迟。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天菊匿,我揣著相機(jī)與錄音付呕,去河邊找鬼。 笑死跌捆,一個(gè)胖子當(dāng)著我的面吹牛徽职,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播佩厚,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼姆钉,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了抄瓦?” 一聲冷哼從身側(cè)響起潮瓶,我...
    開(kāi)封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎钙姊,沒(méi)想到半個(gè)月后毯辅,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡煞额,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年悉罕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片立镶。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡壁袄,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出媚媒,到底是詐尸還是另有隱情嗜逻,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布缭召,位于F島的核電站栈顷,受9級(jí)特大地震影響逆日,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜萄凤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一室抽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧靡努,春花似錦坪圾、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至漾月,卻和暖如春病梢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背梁肿。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工蜓陌, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人吩蔑。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓钮热,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親哥纫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子霉旗,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容