Python 3 file modification implementation is similar to the sed function instance code in shell

Source: Internet
Author: User
This article describes how to modify the Python 3 file to implement the function instance code similar to sed in shell, interested friends can refer to this article for details about how to modify the Python 3 file to implement the function instance code similar to sed in shell. interested friends can refer

# Auther: Aaron Fan ''' There are three file opening modes: r and read-only (default ). W, write-only mode. [Unreadable; created if not exist; deleted if Yes; because the content of the original file is cleared, use it with caution] a, append mode. [Readable; create if no content exists; append content if Yes;] remember to close the file: f. close () python can only write strings to text files. To store numeric data in text, you must first use the str () function to convert it to the string format. # R mode (read-only mode) f = open ('Yesterday', encoding = 'utf-8') data = f. read () f. close () print (data) # first five rows of the read-only File: f = open ('Yesterday', 'R', encoding = 'utf-8 ') for I in range (5): print (f. readline () # read this file in list mode f = open ('Yesterday', 'R', encoding = 'utf-8') for line in f. readlines (): print (line) # make a special print on the 3rd rows of the file (this method is inefficient because it will first read all the files into the memory, when the file is large, it will affect the performance.) f = open ('Yesterday', 'R', encoding = 'utf-8') for index, line in enumerate (f. readlin Es (): if index = 2: print ('----- test line ----', line) continue print (line) # It is more efficient to read file content row by row, because it reads files in one row and one row, it does not read all files in the memory at a time f = open ('Yesterday', 'R', encoding = 'utf-8 ') for line in f: print (line) # relatively efficient, special printing on the three lines of the file f = open ('Yesterday', 'R ', encoding = 'utf-8') count = 0for line in f: if count = 2: print ('------ test line ----:', line) count + = 1 print (line) # w mode (if no file exists, clear the file if any) f = open ('Yesterday', 'W', encodin G = 'utf-8') f. write ("This is a row of test \ n") f. write ("test 2 \ n") f. write ("test 3") # a mode (append mode. If no file exists, append the content if yes) f = open ('Yesterday', 'A ', encoding = 'utf-8') f. write ("test1 \ n") # file cursor operation f = open ('Yesterday', 'R', encoding = 'utf-8 ') # obtain the file cursor print (f. tell () print (f. readline () print (f. tell () # return to the initial position (you can also specify a position, provided that you have to know where the character you want to go to is located) f. seek (0) # display the file encoding print (f. encoding) # display the file handle number (I am not sure whether this statement is correct, please check it carefully when using it) print (f. fileno () # test whether a terminal is set Backup file print (f. isatty () # refresh the cached content to the hard disk (there is an example written in the script on the progress bar) f. flush () # specifies where the file is truncated. if there is no parameter, the file is cleared from 0 by default. # f. truncate () # r + mode (read and append mode) f = open ('Yesterday', 'R + ', encoding = 'utf-8') data = f. read () print (data) f. write ("test... \ n ") # w + WRITE-read mode, a + append-read mode, which is generally not used. if it is used, let's get to know # rb mode, read this file in binary mode # wb mode. write the ''''' # with statement in binary mode (very practical. remember to use it frequently ~, The file and exception chapter in the book "Python programming from getting started to practice" has detailed usage) # to avoid forgetting to close the file after opening it, you can manage the context, that is: with open ('log', 'r') as f :... # In this way, when the with code block is executed, the file resources are automatically closed and released internally. # After Python 2.7, with supports managing the context of multiple files at the same time, that is, with open ('log1') as obj1, open ('log2') as obj2: pass '''

The above is the detailed content of the Python 3 file modification implementation instance code similar to the sed function in shell. For more information, see other related articles in the first PHP community!

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.