注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

More Than Programmer

do more and more,beyond programmer... ..

 
 
 

日志

 
 
关于我

每天进步一点,让自己活得充实!

网易考拉推荐
 
 

Python文件操作  

2008-11-08 10:17:47|  分类: Python |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Reading and Writing

 

The most basic tasks involved in file manipulation are reading data from files and writing data to files. This is a very simple task to learn. Let's open a file for writing: 

fileHandle = open ( 'test.txt', 'w' ) 

The "w" indicates that we will be writing to the file, and the rest is pretty simple to understand. The next step is to write data to the file: 

fileHandle.write ( 'This is a test.\nReally, it is.' ) 

This will write the string "This is a test." to the file's first line and "Really, it is." to the file's second line. Finally, we need to clean up after ourselves and close the file: 

fileHandle.close() 

As you can see, it's very easy, especially with Python's object orientation. Note that when you use the "w" mode to write to the file again, all its contents will be deleted. To get past this, use the "a" mode to append data to a file, adding data to the bottom: 

fileHandle = open ( 'test.txt', 'a' )

fileHandle.write ( '\n\n\nBottom line.' )

fileHandle.close() 

Now let's read our file and display the contents: 

fileHandle = open ( 'test.txt' )

print fileHandle.read()

fileHandle.close() 

This will read the entire file and print the data within it. We can also read a single line in the file: 

fileHandle = open ( 'test.txt' )

print fileHandle.readline() # "This is a test."

fileHandle.close() 

It is also possible to store the lines of a file into a list: 

fileHandle = open ( 'test.txt' )

fileList = fileHandle.readlines()

for fileLine in fileList:

   print '>>', fileLine

fileHandle.close() 

When reading a file, Python's place in the file will be remembered, illustrated in this example: 

fileHandle = open ( 'test.txt' )

garbage = fileHandle.readline()

fileHandle.readline() # "Really, it is."

fileHandle.close() 

Only the second line is displayed. We can, however, get past this by telling Python to resume reading from a different position: 

fileHandle = open ( 'test.txt' )

garbage = fileHandle.readline()

fileHandle.seek ( 0 )

print fileHandle.readline() # "This is a test."

fileHandle.close() 

In the above example, we tell Python to continue reading from the first byte in the file. Thus, the first line is printed. We can also request Python's place within the file: 

fileHandle = open ( 'test.txt' )

print fileHandle.readline() # "This is a test."

print fileHandle.tell() # "17"

print fileHandle.readline() # "Really, it is." 

It is also possible to read the file a few bytes at a time: 

fileHandle = open ( 'test.txt' )

print fileHandle.read ( 1 ) # "T"

fileHandle.seek ( 4 )

print FileHandle.read ( 1 ) # "T" 

When working with Windows and Macintosh, sometimes you are required to read and write files in binary mode, such as images or executional files. To do this, simply append "b" to the file mode: 

fileHandle = open ( 'testBinary.txt', 'wb' )

fileHandle.write ( 'There is no spoon.' )

fileHandle.close()

 

fileHandle = open ( 'testBinary.txt', 'rb' )

print fileHandle.read()

fileHandle.close()

  评论这张
 
阅读(1263)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017