Section Sixth: Python file processing

Source: Internet
Author: User

File (' Test.txt ', ' r ')         read-only mode, is also the default way. (If the Test.txt file does not exist, it will error.) ) file (' Test.txt ', ' W ')         write a file. (If you do not have this file, a file will be created in the current directory.) file (' Test.txt ', ' a ')          Append method. File (' Test.txt ', ' W '). Write (' abcdefghkhjisorg ')    put ABC ... Writes a test.txt file. The way it is written overwrites the old file. F=open ('/tmp/hello ', ' W ') Open (path + file name, read-write mode)  with open (' Test.txt ', ' R ') as f:        #这种方式写文件, do not close automatically save after writing. For i in f.readlines ():                         print i   -------- ---------------------------------------------f=file (' Test.txt ', ' W ')                                #创建一个文件, the type is written to open f.write (' TODAY IS A&NBsp;good day\n ')       #换行  f.close ()                                             #   Close and save (to be closed in R). import tab                                         #导入tab键f. Read ()                                               #读入写入的内容f. Flush ()                                               #刷新写入磁盘 (closed in R) F.tell ()                                                # Displays the first few characters now. F.seek (0)                                           #移到指定的字符位置. For example, move to a 33rd character. F.seek () f.readline ()                                        #换行的方式读, read one line at a time. F.readlines ()                                     #以列表的方式读全部内容content =f.read ()                              #把文件读的方式传给contentcontent                                            # View content Contents Content.split (' \ n ') [0]                       #分行的方式查看文件, [0], the first line is viewed. [1] When viewing the second line. C=f.readlines ()                               #cc [0]c[1] Modify the replacement file:  for  i in fileinput.input (' A.log ', inplace=1):      i=i.replace (' Break ', ' Continue ')       print i first back up and then modify the file:   for  i in fileinput.input (' A.log ', inplace=1,backup= '. Bak '):      i=i.replace (' Break ', ' Continue ')       print i


This article from "Small East elder brother" blog, declined reprint!

Section Sixth: Python file processing

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.