Python implements file content substitution similar to SED commands

Source: Internet
Author: User

#!/usr/bin/env python #_ *_coding:utf-8 _*_ #replace () method replaces the old string in the string with the new one, and if the third parameter max is specified, the substitution does not exceed Max times.     #语法: Str.replace (old, new[, Max]) import Sys,os old_text, new_text, file_name = sys.argv[1], sys.argv[2], sys.argv[3] f = File (file_name, ' rb ') New_file = file ('%s.bak '% file_name, ' WB ') for line in F.xreadlines (): New_file.writ E (Line.replace (Old_text,new_text)) F.close () New_file.close ()
Important note sys.argv[1] #参数1sys. argv[2] #参数2sys. argv[3] #参数3 The #replace () method replaces the old (older string) in the string with the new (string), and if the third parameter max is specified, the substitution does not exceed Max Times. #语法: Str.replace (old, new[, Max]) usage method: Python code2.py ' raw string ' new string ' file


This article is from the "Fa&it-Q Group: 223843163" blog, please be sure to keep this source http://freshair.blog.51cto.com/8272891/1869489

Python implements file content substitution similar to SED commands

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.