使用Python讀寫(xiě)文本文件及編寫(xiě)簡(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)容,我們不在代碼中寫(xiě)死文件名稱,因?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) -- 寫(xiě)入文件。
這些是你應(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è)辦法是,先寫(xiě)幾行,運(yùn)行一下,可以運(yùn)行再寫(xiě)幾行,直到都可以運(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中的iterator和"lazy?iterator"區(qū)別介紹
這篇文章主要介紹了python中的iterator和?“l(fā)azy?iterator“之間有什么區(qū)別,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
Pytorch中torch.stack()函數(shù)的深入解析
在pytorch中常見(jiàn)的拼接函數(shù)主要是兩個(gè),分別是:stack()和cat(),下面這篇文章主要給大家介紹了關(guān)于Pytorch中torch.stack()函數(shù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Python之random.sample()和numpy.random.choice()的優(yōu)缺點(diǎn)說(shuō)明
這篇文章主要介紹了Python之random.sample()和numpy.random.choice()的優(yōu)缺點(diǎn)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
python百行代碼實(shí)現(xiàn)漢服圈圖片爬取
這篇文章主要為大家介紹了使用python百行代碼來(lái)實(shí)現(xiàn)漢服圈的圖片爬取,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
python?基于aiohttp的異步爬蟲(chóng)實(shí)戰(zhàn)詳解
這篇文章主要為大家介紹了python?基于aiohttp的異步爬蟲(chóng)實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Python字典刪除鍵值對(duì)和元素的四種方法(小結(jié))
刪除列表或者字符串元素的方法不止一種,同樣,刪除字典元素的方法也不止一種,本文主要介紹python中刪除字典元素的四種方法:1、使用del語(yǔ)句;2、使用clear();3、使用pop();4、使用popitem()。感興趣的可以了解一下2021-12-12
python中asyncore異步模塊的實(shí)現(xiàn)
本文主要介紹了python中asyncore異步模塊的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

