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

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

系統 1864 0

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

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

            
fileObject.writelines( sequence )


          

參數

  • ??? 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()


          

當我們運行上面的程序,它會產生以下結果:

            
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


          



更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 同心县| 河南省| 房山区| 迭部县| 天台县| 惠东县| 夏河县| 江永县| 平凉市| 海丰县| 赫章县| 石狮市| 黑水县| 屏南县| 湘潭县| 招远市| 荔浦县| 武义县| 兴安盟| 广南县| 句容市| 乌苏市| 临桂县| 黔江区| 赣榆县| 泽库县| 邯郸市| 镇原县| 老河口市| 凉山| 水城县| 剑阁县| 靖边县| 台州市| 定安县| 荥阳市| 蒙阴县| 大关县| 新巴尔虎右旗| 界首市| 静安区|