激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python中操作文件之write()方法的使用教程

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

2020-07-06 13:17腳本之家 Python

這篇文章主要介紹了Python中操作文件之write()方法的使用教程,是Python入門學習中的基礎知識,需要的朋友可以參考下

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

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

?
1
fileObject.write( str )

參數(shù)

  •     str -- 這是要被寫入的文件中的字符串。

返回值

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/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()

當我們運行上面的程序,它會產(chǎn)生以下結(jié)果:

?
1
2
3
4
5
6
7
8
9
10
11
12
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文字幕11| 欧美成人精品 | 欧美特级一级毛片 | 欧美成人一区免费视频 | 精品久久久久久中文字幕 | 国产一级桃视频播放 | 蜜桃久久一区二区三区 | 国产日韩线路一线路二 | 日韩电影av在线 | 欧美一区2区三区4区公司二百 | 国产一级二级在线播放 | 色97色 | 国外成人在线视频网站 | 欧美一级黑人 | 中文字幕在线播放视频 | 欧美日韩在线看片 | 色吧久久 | 私库av在线免费观看 | 极品美女一级毛片 | fc2成人免费人成在线观看播放 | 国产精品久久久久久模特 | 欧美性生活久久 | 在线无码| 一区二区精品视频 | 午夜精品福利视频 | 最新中文字幕第一页视频 | 黄色片网站在线免费观看 | 毛片大全免费 | 深夜福利视频绿巨人视频在线观看 | 欧美成人se01短视频在线看 | 天天操天天骑 | 日韩中文字幕一区二区三区 | 欧美激情第一区 | 国产一级大片在线观看 | 超级av在线 | 色七七亚洲 | 精品国产一区二区三区免费 | 一本大道av | 性爱在线免费视频 | 国产精品99久久久久久大便 | 国产福利不卡一区二区三区 |