#!/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