Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'args': 0.04; 'case.': 0.05; 'modify': 0.05; 'completeness': 0.07; 'e.g.,': 0.07; 'escape': 0.07; 'function,': 0.07; 'properly.': 0.07; 'reason,': 0.07; 'repeated': 0.07; 'works.': 0.07; "'''": 0.09; 'received:155': 0.09; 'windows,': 0.09; 'def': 0.10; "'\\''": 0.16; 'bourne': 0.16; 'commandline': 0.16; 'concatenate': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'pipes': 0.16; 'received:155.180': 0.16; 'received:155.180.234': 0.16; 'received:155.180.234.122': 0.16; 'received:159.53': 0.16; 'received:159.53.110': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:se3.svr.bankone.net': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'sees': 0.16; 'somehow.': 0.16; 'stop.': 0.16; 'strs': 0.16; 'subprocess': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'string': 0.17; 'wrote:': 0.17; 'shell': 0.18; '>>>': 0.18; 'module': 0.19; 'skip:" 30': 0.20; 'to:name:python-list@python.org': 0.20; 'import': 0.21; 'received:169.254': 0.24; 'least': 0.25; 'header :In-Reply-To:1': 0.25; 'accuracy': 0.27; 'replace': 0.27; 'lines': 0.28; 'case,': 0.29; 'received:169': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'file': 0.32; 'could': 0.32; 'cases,': 0.33; 'to:addr :python-list': 0.33; 'text': 0.34; 'feed': 0.35; 'replaced': 0.35; 'except': 0.36; 'but': 0.36; 'should': 0.36; 'too': 0.36; 'charset :us-ascii': 0.36; 'possible': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'containing': 0.61; "you'll": 0.62; 'thomas': 0.62; 'information,': 0.63; 'url:email': 0.63; 'legal': 0.65; 'subject': 0.66; 'purchase': 0.67; 'sale': 0.76; 'alternative.': 0.84; 'expressive': 0.84; 'rachel': 0.84; 'received:169.254.8': 0.84; 'subject:better': 0.84; 'ugly,': 0.84 X-DKIM: OpenDKIM Filter v2.1.3 sj3.jpmchase.com q9TMSxwS001424 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1351549739; bh=ZrAX1rGpjzCw81MaIElNYnd7RH/+Y6WYzBldLnyHiTo=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=PS5CHyXzzIPDccjx+qUQQUFIQQ75jnnVsAU8Ifqmcx+rvX9cg9//EcDDpWi/zdoGp nkcQXGZQw3A/d8m7X0MxE3lAql3NGHL+ZwGhL4ozTlxtgydzuEfZ4ZOjJT0bR+8/GX 2CnMV4YcAuUD5MfQFjh3ZsyO6eoLtQWQe2N2E0bM= From: "Prasad, Ramit" To: "python-list@python.org" Subject: RE: better way for ' '.join(args) + '\n'? Thread-Topic: better way for ' '.join(args) + '\n'? Thread-Index: AQHNs1K64g32Hn/c8EOwCkd/YMd20JfNJsYAgAO5rzA= Date: Mon, 29 Oct 2012 22:28:52 +0000 References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.67.79.47] Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-DLP-FWD: Yes Content-Type: text/plain; charset="us-ascii" 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351549748 news.xs4all.nl 6962 [2001:888:2000:d::a6]:42022 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32429 Thomas Rachel wrote:=0D=0A> Am 26=2E10=2E2012 09:49 schrieb Ulrich Eckhardt= :=0D=0A> > Hi!=0D=0A> >=0D=0A> > General advise when assembling strings is = to not concatenate them=0D=0A> > repeatedly but instead use string's join()= function, because it avoids=0D=0A> > repeated reallocations and is at leas= t as expressive as any alternative=2E=0D=0A> >=0D=0A> > What I have now is = a case where I'm assembling lines of text for driving=0D=0A> > a program wi= th a commandline interface=2E=0D=0A> =0D=0A> Stop=2E=0D=0A> =0D=0A> In this= case, you think too complicated=2E=0D=0A> =0D=0A> Just do=0D=0A> =0D=0A> = subprocess=2EPopen(['prog', 'foo', 'bar', 'baz'])=0D=0A> =0D=0A> - is = the most safest thing for this use case=2E=0D=0A> =0D=0A> If it should not = be possible for any reason, you should be aware of any=0D=0A> traps you cou= ld catch - e=2Eg=2E, if you want to feed your string to a=0D=0A> Bourne she= ll, you should escape the strings properly=2E=0D=0A> =0D=0A> In such cases,= I use=0D=0A> =0D=0A> =0D=0A> def shellquote(*strs):=0D=0A> r"""Input: fil= e names, output: ''-enclosed strings where every ' is=0D=0A> replaced with = '\''=2E Intended for usage with the shell=2E"""=0D=0A> # just take over ev= erything except ';=0D=0A> # replace ' with '\''=0D=0A> # The shell sees '= '' as ''\'''\'''\'''=2E Ugly, but works=2E=0D=0A> return " "=2Ejoin([=0D= =0A> "'"+st=2Ereplace("'","'\\''")+"'"=0D=0A> for st in strs=0D=0A> ])= =0D=0A> =0D=0A> =0D=0A> so I can use=0D=0A> =0D=0A> shellquote('program nam= e', 'argu"ment 1', '$arg 2',=0D=0A> "even args containing a ' are ok")= =0D=0A> =0D=0A> For Windows, you'll have to modify this somehow=2E=0D=0A> = =0D=0A=0D=0AThe subprocess module suggests using pipes=2Equote for escaping= =2E=0D=0A=0D=0A>>> a=0D=0A('program name', 'argu"ment 1', '$arg 2', "even a= rgs containing a ' are ok")=0D=0A>>> import pipes=0D=0A>>> map(pipes=2Equot= e, a)=0D=0A["'program name'", '\'argu"ment 1\'', "'$arg 2'", '\'even args c= ontaining a \'"\'"\' are ok\'']=0D=0A>>> ' '=2Ejoin(a)=0D=0A'\'program name= \' \'argu"ment 1\' \'$arg 2\' \'even args containing a \'\\\'\' are ok\''= =0D=0A=0D=0A=0D=0ARamit Prasad=0D=0A=0D=0A=0D=0AThis email is confidential = and subject to important disclaimers and=0D=0Aconditions including on offer= s for the purchase or sale of=0D=0Asecurities, accuracy and completeness of= information, viruses,=0D=0Aconfidentiality, legal privilege, and legal ent= ity disclaimers,=0D=0Aavailable at http://www=2Ejpmorgan=2Ecom/pages/disclo= sures/email=2E