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

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

系統(tǒng) 1801 0

?write()方法把字符串str寫入文件。沒有返回值。由于緩沖,字符串可能不實(shí)際顯示文件,直到flush()或close()方法被調(diào)用。
語法

以下是write()方法的語法:

            
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)行上面的程序,它會產(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號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 宁德市| 郁南县| 三河市| 娱乐| 广水市| 潞城市| 于田县| 积石山| 集贤县| 兴义市| 虎林市| 光泽县| 大洼县| 离岛区| 潮安县| 许昌市| 女性| 奉化市| 景泰县| 田阳县| 合水县| 东乌珠穆沁旗| 郑州市| 红原县| 余江县| 孟连| 南平市| 长治县| 祁连县| 翁牛特旗| 阿城市| 泗洪县| 兴安盟| 临夏市| 昌邑市| 凌海市| 城口县| 石河子市| 柳江县| 高雄市| 蓬安县|