Python file Operation class Walkthrough

Source: Internet
Author: User
This article describes the operation of the Python file operation class, the detailed code is as follows:

#!/usr/bin/env python#!/usr/bin/env python #coding: Utf-8 # Purpose: File Operation class # declares a string literal poem= ' ' programming is a fun test when the Work is do if you wanna make your work also fun:use python! "#创建一个file类的实例, the mode can be: 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 write F.close () #关闭文件 Close the file# default is read-only mode f=file (' Poem.txt ') # If no mode is specified, ' R ' ead mode is assumed by default while True:line=f.readline () #读取文件的每一个行 If Len ==0: # Zero length indicates EOF break print L INE, #输出该行 #注意 because the content read from the file already ends with a newline character, so we use commas on the output statement to eliminate the wrapping.  #Notice comma to avoid automatic newline added by Python f.close () #close the file# Assist 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.