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

詳解詳解Python中writelines()方法的使用

系統(tǒng) 1961 0

?writelines()方法寫入字符串序列到文件。該序列可以是任何可迭代的對象產(chǎn)生字符串,字符串為一般列表。沒有返回值。
語法

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

            
fileObject.writelines( sequence )


          

參數(shù)

  • ??? sequence -- 這是字符串的序列。

返回值

此方法不返回任何值。
例子

下面的例子顯示writelines()方法的使用。

            
#!/usr/bin/python'

# Open a file in witre 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

seq = ["This is 6th line\n", "This is 7th line"]
# Write sequence of lines at the end of the file.
fo.seek(0, 2)
line = fo.writelines( seq )

# Now read complete file from beginning.
fo.seek(0,0)
for index in range(7):
  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

Line No 6 - This is 7th line


          



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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 福安市| 迁西县| 石家庄市| 济阳县| 祁东县| 荔浦县| 和静县| 静乐县| 新兴县| 自治县| 宕昌县| 沁源县| 宜兰县| 凤山市| 岐山县| 乡城县| 汶上县| 革吉县| 揭阳市| 龙江县| 米易县| 丽水市| 新沂市| 蓝山县| 大丰市| 油尖旺区| 仲巴县| 宣武区| 柘城县| 正定县| 洮南市| 彭泽县| 桑植县| 潢川县| 静安区| 万荣县| 明星| 丰原市| 石渠县| 五常市| 阿瓦提县|