Python file Action class operation instance code

Source: Internet
Author: User

  #!/usr/bin/env python

#!/usr/bin/env python 

#coding: utf-8 

# Auth Or: Cool

Purpose: File action class  

# created:2011/1/1 


#声明一个字符串文本 &N Bsp

poem= '  

programming is fun test  

When the work is done 

One if You are wanna make your work also fun: 

use python! 


#创建一个file类的实例, mode Thought: Read-only mode (' R '), write mode (' W '), Append mode (' a ')  

F=file (' poem.txt ', ' a ') #open for ' W ' riting 

F.write ( Poem) #写入文本到文件 write text to file 

F.close () #关闭文件 close the file 


#默认 is a read-only mode  

f=file (' poem.txt ')  

# If no # is specified, ' r ' ead the mode is assumed by DEFAULT&N Bsp

While true: 

Line=f.readline () #读取文件的每一个行  

-If Len (line) ==0: # Zero Length I Ndicates eof 


Print line, #输出该行  

#注意, because the content read from the file has ended with a newline character, we use commas on the output statement to eliminate word wrapping. &NBSP


#Notice comma to avoid automatic newline added by python 

F.close () #close the file 




Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.