日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python中操作文件之write()方法的使用教程

系統(tǒng) 1718 0

?write()方法把字符串str寫入文件。沒有返回值。由于緩沖,字符串可能不實(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


          



更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。。?/p>

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 陆丰市| 云浮市| 定襄县| 宜兰县| 郯城县| 夏河县| 永寿县| 沙田区| 大兴区| 武清区| 白银市| 东莞市| 恩施市| 成安县| 思南县| 泸定县| 岱山县| 金昌市| 涟水县| 梅州市| 庄河市| 镇雄县| 西盟| 青冈县| 南通市| 蒙阴县| 武平县| 罗城| 鹿泉市| 霍州市| 河津市| 宁陕县| 灵丘县| 北票市| 唐山市| 德保县| 江永县| 合肥市| 新闻| 苏尼特右旗| 射洪县|