Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32269
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: better way for ' '.join(args) + '\n'? |
| Date | 2012-10-27 11:26 +0200 |
| Organization | A newly installed InterNetNews server |
| Message-ID | <k6g9cm$t47$1@r03.glglgl.gl> (permalink) |
| References | <utrpl9-udk.ln1@satorlaser.homedns.org> |
Am 26.10.2012 09:49 schrieb Ulrich Eckhardt:
> Hi!
>
> General advise when assembling strings is to not concatenate them
> repeatedly but instead use string's join() function, because it avoids
> repeated reallocations and is at least as expressive as any alternative.
>
> What I have now is a case where I'm assembling lines of text for driving
> a program with a commandline interface.
Stop.
In this case, you think too complicated.
Just do
subprocess.Popen(['prog', 'foo', 'bar', 'baz'])
- is the most safest thing for this use case.
If it should not be possible for any reason, you should be aware of any
traps you could catch - e.g., if you want to feed your string to a
Bourne shell, you should escape the strings properly.
In such cases, I use
def shellquote(*strs):
r"""Input: file names, output: ''-enclosed strings where every ' is
replaced with '\''. Intended for usage with the shell."""
# just take over everything except ';
# replace ' with '\''
# The shell sees ''' as ''\'''\'''\'''. Ugly, but works.
return " ".join([
"'"+st.replace("'","'\\''")+"'"
for st in strs
])
so I can use
shellquote('program name', 'argu"ment 1', '$arg 2',
"even args containing a ' are ok")
For Windows, you'll have to modify this somehow.
HTH,
Thomas
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
better way for ' '.join(args) + '\n'? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-26 09:49 +0200
Re: better way for ' '.join(args) + '\n'? Peter Otten <__peter__@web.de> - 2012-10-26 10:58 +0200
Re: better way for ' '.join(args) + '\n'? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 09:36 +0000
Re: better way for ' '.join(args) + '\n'? Hubert Grünheidt <hgruenheidt@t-online.de> - 2012-10-26 18:21 +0200
Re: better way for ' '.join(args) + '\n'? Tycho Andersen <tycho@tycho.ws> - 2012-10-26 16:26 -0500
Re: better way for ' '.join(args) + '\n'? Dave Angel <d@davea.name> - 2012-10-26 17:36 -0400
Re: better way for ' '.join(args) + '\n'? Tycho Andersen <tycho@tycho.ws> - 2012-10-26 16:42 -0500
Re: better way for ' '.join(args) + '\n'? Ramchandra Apte <maniandram01@gmail.com> - 2012-11-03 01:26 -0700
Re: better way for ' '.join(args) + '\n'? Ramchandra Apte <maniandram01@gmail.com> - 2012-11-03 01:26 -0700
Re: better way for ' '.join(args) + '\n'? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-10-27 11:26 +0200
RE: better way for ' '.join(args) + '\n'? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-29 22:28 +0000
csiph-web