Python中操作文件之write()方法的使用教程
write()方法把字符串str寫入文件。沒(méi)有返回值。由于緩沖,字符串可能不實(shí)際顯示文件,直到flush()或close()方法被調(diào)用。
語(yǔ)法
以下是write()方法的語(yǔ)法:
fileObject.write( str )
參數(shù)
- str -- 這是要被寫入的文件中的字符串。
返回值
此方法不返回任何值。
例子
下面的例子顯示write()方法的使用。
#!/usr/bin/python # Open a file in write mode fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line str = "This is 6th line" # Write a line at the end of the file. fo.seek(0, 2) line = fo.write( str ) # Now read complete file from beginning. fo.seek(0,0) for index in range(6): line = fo.next() print "Line No %d - %s" % (index, line) # Close opend file fo.close()
當(dāng)我們運(yùn)行上面的程序,它會(huì)產(chǎn)生以下結(jié)果:
Name of the file: foo.txt Line No 0 - This is 1st line Line No 1 - This is 2nd line Line No 2 - This is 3rd line Line No 3 - This is 4th line Line No 4 - This is 5th line Line No 5 - This is 6th line
相關(guān)文章
將tf.batch_matmul替換成tf.matmul的實(shí)現(xiàn)
這篇文章主要介紹了將tf.batch_matmul替換成tf.matmul的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06pandas中apply和transform方法的性能比較及區(qū)別介紹
這篇文章主要介紹了pandas中apply和transform方法的性能比較,在文中給大家講解了apply() 與transform()的相同點(diǎn)與不同點(diǎn),需要的朋友可以參考下2018-10-10tensorflow 實(shí)現(xiàn)從checkpoint中獲取graph信息
今天小編就為大家分享一篇tensorflow 實(shí)現(xiàn)從checkpoint中獲取graph信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python開(kāi)發(fā)的實(shí)用計(jì)算器完整實(shí)例
這篇文章主要介紹了Python開(kāi)發(fā)的實(shí)用計(jì)算器,結(jié)合完整實(shí)例形式分析了Python實(shí)現(xiàn)計(jì)算器四則運(yùn)算、開(kāi)方、取余等相關(guān)操作技巧,需要的朋友可以參考下2017-05-05python+matplotlib實(shí)現(xiàn)禮盒柱狀圖實(shí)例代碼
這篇文章主要介紹了python+matplotlib實(shí)現(xiàn)禮盒柱狀圖實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01python數(shù)據(jù)持久存儲(chǔ) pickle模塊的基本使用方法解析
這篇文章主要介紹了python數(shù)據(jù)持久存儲(chǔ) pickle模塊的基本使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Python數(shù)據(jù)操作方法封裝類實(shí)例
這篇文章主要介紹了Python數(shù)據(jù)操作方法封裝類,結(jié)合具體實(shí)例形式分析了Python針對(duì)數(shù)據(jù)庫(kù)的連接、執(zhí)行sql語(yǔ)句、刪除、關(guān)閉等操作技巧,需要的朋友可以參考下2017-06-06使用python分析統(tǒng)計(jì)自己微信朋友的信息
這篇文章主要介紹了python分析統(tǒng)計(jì)自己微信朋友的信息,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07