Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '16,': 0.03; 'subject:file': 0.07; 'if,': 0.09; 'output,': 0.09; 'parsing': 0.09; 'subject:skip:c 10': 0.09; 'python': 0.11; 'configuring': 0.16; 'for,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'garg': 0.16; 'rules.': 0.16; 'subject:edit': 0.16; 'files.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'file,': 0.19; 'not,': 0.20; 'server.': 0.24; 'file.': 0.24; "i've": 0.25; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'description,': 0.31; 'file': 0.32; 'this.': 0.32; 'figure': 0.32; 'languages': 0.32; 'text': 0.33; 'not.': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'consistent': 0.36; 'done': 0.36; 'so,': 0.37; 'nov': 0.38; 'to:addr:python-list': 0.38; 'track': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'easy': 0.60; 'identify': 0.61; 'matter': 0.61; 'simply': 0.61; "you're": 0.61; 'times': 0.62; "you'll": 0.62; 'complete': 0.62; 'subject:Best': 0.91; 'this;': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=ORMLJdWG5J00EjIKZPgfq+m5K+g7l7aY2b95iVqB7ic=; b=stUcd6/FBVdQZ2jkvlQom/bT+zTB+2z7isG31w+1FQFSPLaHIFRTlTXM+ulHfLbNvp ii/TJS6eqz8fOcMW/Nht5OKjwr75QHZ4Gylq9IR0dsLhWcad/N5UdJA2bsZsLicoJhTk x5hZUPzdsTIbwJ2e1EVrMBM5vWdhHt3GpIZpyqrPTinTUBEB7PO4Xn+V32Up317TmZUd +OFNIeIhRM95GB983j1guvhwKqoDUc3WgC7rR6g/cIMLWIFJ3S1yYDlu/521PxJ24Z0K iHc6IGvahtU+gfFduIFmozvepEczmtUVjBP4pb50hUNWbUt/ipmcNhpJzSA68QpiFtMP PSfg== MIME-Version: 1.0 X-Received: by 10.66.2.234 with SMTP id 10mr6823449pax.39.1384521361648; Fri, 15 Nov 2013 05:16:01 -0800 (PST) In-Reply-To: References: Date: Sat, 16 Nov 2013 00:16:01 +1100 Subject: Re: Best approach to edit linux configuration file From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384521369 news.xs4all.nl 15875 [2001:888:2000:d::a6]:52221 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59515 On Sat, Nov 16, 2013 at 12:05 AM, Himanshu Garg wrote: > I have to setup the DNS server. For this I have to edit the configuration files. > > For this I have to search if the lines(block of text) already exist in the file and if not, I have to add them to the file. > > So, I want to know what is the best way to accomplish this. Is your script allowed to take complete control of the file, or do you have to cope with human edits? If you CAN take control, things are easy. Just keep track of your own content and match exact lines; as long as you always make consistent output, you can look for those lines precisely. But if, as I suspect from your (scanty) description, you can't, then you'll need to figure out how to identify whether the lines exist or not. That means text parsing rules. Python can definitely do this; it's simply a matter of figuring out what you're looking for, what you're adding, etc. Configuring DNS is pretty easy for a script to do. I've done it several times (though only once in Python - other languages other times). ChrisA