使用Python讀寫文本文件及編寫簡(jiǎn)單的文本編輯器
學(xué)習(xí)raw_input和argv是學(xué)習(xí)讀取文件的前提,你可能不能完全理解這個(gè)練習(xí),所以認(rèn)真學(xué)習(xí)并檢查。如果不認(rèn)真的話,很容易刪除一些有用的文件。
這個(gè)練習(xí)包含兩個(gè)文件,一個(gè)是運(yùn)行文件ex15.py,一個(gè)是ex15_sample.txt。第二個(gè)文件不是腳本文件,只包括一些文本,如下:
This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here.
我們要做的就是打開(kāi)這個(gè)文件,然后打印文件內(nèi)容,我們不在代碼中寫死文件名稱,因?yàn)槲覀內(nèi)绻x取其他文件的話,就要重新修改代碼,解決這個(gè)問(wèn)題的辦法就是使用argv和raw_input。
from sys import argv script, filename = argv txt = open(filename) print "Here's your file %r:" % filename print txt.read() print "Type the filename again:" file_again = raw_input("> ") txt_again = open(file_again) print txt_again.read()
上面的代碼做了一些有意思的事情,讓我們快速的分解一下:
1-3行使用argv取得文件名。第5行使用open命令,現(xiàn)在使用pydoc open看看這個(gè)命令的介紹。
第7行打印一行信息,但是第8行有一些新的東西。我們?cè)趖xt上調(diào)用了一個(gè)方法。我們通過(guò)open方法得到一個(gè)file,這個(gè)file有一些我們可以調(diào)用的方法。使用這些方法的方法就是在file后面加一個(gè).(點(diǎn)),比如txt.read(),就像是說(shuō):“嘿,執(zhí)行讀取命令,沒(méi)有任何參數(shù)!”
剩下部分大家在加分練習(xí)中分析吧。
運(yùn)行結(jié)果
root@he-desktop:~/mystuff# python ex15.py ex15_sample.txt
Here's your file 'ex15_sample.txt': This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here. Type the filename again: > ex15_sample.txt This is stuff I typed into a file. It is really cool stuff. Lots and lots of fun to have in here.
下面幾個(gè)文件的命令比較常用:
- close -- 關(guān)閉文件,相當(dāng)于編輯器中的File->Save
- read -- 讀取文件內(nèi)容分配給一個(gè)變量
- readline -- 讀取一行內(nèi)容
- truncate -- 清空文件,小心使用這個(gè)命令
- write(stuff) -- 寫入文件。
這些是你應(yīng)該知道的重要命令,只有write需要提供參數(shù)。
讓我們使用這些命令實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文本編輯器。
from sys import argv script, filename = argv print "We're going to erase %r." % filename print "If you don't want that, hit CTRL-C (^C)." print "If you do want that, hot RETURN." raw_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 = raw_input("line 1: ") line2 = raw_input("line 2: ") line3 = raw_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()
這個(gè)程序比較長(zhǎng),所以慢慢來(lái),讓它能運(yùn)行起來(lái)。有個(gè)辦法是,先寫幾行,運(yùn)行一下,可以運(yùn)行再寫幾行,直到都可以運(yùn)行。
運(yùn)行結(jié)果
你會(huì)看到兩個(gè)東西,一個(gè)是程序的輸出:
root@he-desktop:~/mystuff# python ex16.py test.txt
We're going to erase 'test.txt'. If you don't want that, hit CTRL-C (^C). If you do want that, hot RETURN. ? Opening the file... Truncating the file. Goodbye!! Now I'm going to ask you for three lines. line 1: Hi! line 2: Welcome to my blog! line 3: Thank you! I'm going to write these to the file. And finally, we close it.
還有就是你新建立的文件,打開(kāi)看看吧。
相關(guān)文章
Python應(yīng)用利器之緩存機(jī)制的妙用詳解
在 Python 應(yīng)用程序中,使用緩存能夠顯著提高性能并降低資源消耗,本文將詳細(xì)介紹如何在 Python 中實(shí)現(xiàn)緩存機(jī)制,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12通過(guò)Python繪制中國(guó)結(jié)的示例代碼
再過(guò)不久就要到新年了,所以這篇文章將為大家介紹一下如何通過(guò)Python代碼繪制一個(gè)中國(guó)結(jié),文中的示例代碼講解詳細(xì),感興趣的可以動(dòng)手試一試2022-01-01利用Vscode進(jìn)行Python開(kāi)發(fā)環(huán)境配置的步驟
這篇文章主要給大家介紹了關(guān)于如何利用Vscode進(jìn)行Python開(kāi)發(fā)環(huán)境配置的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Python Django框架實(shí)現(xiàn)應(yīng)用添加logging日志操作示例
這篇文章主要介紹了Python Django框架實(shí)現(xiàn)應(yīng)用添加logging日志操作,結(jié)合實(shí)例形式分析了Django框架中添加Python內(nèi)建日志模塊相關(guān)操作技巧,需要的朋友可以參考下2019-05-05es+flask搜索小項(xiàng)目實(shí)現(xiàn)分頁(yè)+高亮的示例代碼
本文主要介紹了es+flask搜索小項(xiàng)目實(shí)現(xiàn)分頁(yè)+高亮的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01