Python file Operation class Operation example detailed _python

Source: Internet
Author: User
Tags word wrap

This article describes an operation instance of the Python file action class with detailed code as follows:

#!/usr/bin/env python
#!/usr/bin/env python 
#coding: Utf-8 
# Purpose: File operation class

#声明一个字符串文本 
poem= ' " 
programming is fun test 
When the work are done 
if your wanna make your work also fun: Use 
python! 
' 
' #创建一个file类的实例, the pattern can be read-only (' 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

#默认是只读模式 
f=file (' Poem.txt ') 
# if no m Ode is specified, ' R ' ead "mode is assumed by default" while 
True: 
line=f.readline () #读取文件的每一个行 
If len = =0: # Zero length indicates EOF 
breaks print line, #输出该行 
#注意 because the content read from the file has ended with a newline character, so we use commas on the output statement to eliminate the word wrap. 
 
#Notice comma to avoid automatic newline added by Python 
F.close () #close the file

#帮助 help 
(file)
Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.