Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!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; 'syntax': 0.03; 'output': 0.04; 'error:': 0.05; 'things.': 0.05; 'bash': 0.07; 'filename': 0.07; 'filenames': 0.07; 'works.': 0.07; 'python': 0.09; '"my': 0.09; 'cmd': 0.09; 'incorrect': 0.09; 'indeed,': 0.09; 'literal': 0.09; 'received:mail-vb0-f46.google.com': 0.09; 'sake': 0.09; 'sep': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; "wouldn't": 0.11; 'resulting': 0.13; 'cases': 0.15; '"cat': 0.16; "'my": 0.16; 'backslashes': 0.16; 'invocations': 0.16; 'magic.': 0.16; 'naive': 0.16; 'subprocess': 0.16; 'string': 0.17; 'wrote:': 0.17; 'fix': 0.17; 'element': 0.17; 'pfxlen:0': 0.17; '>>>': 0.18; 'received:209.85.212.46': 0.18; 'causing': 0.20; 'equivalent': 0.20; 'import': 0.21; 'fine,': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'am,': 0.27; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'rest': 0.28; 'received:209.85.212': 0.28; 'chris': 0.28; 'run': 0.28; 'cat': 0.29; 'invoke': 0.29; 'no,': 0.29; "skip:' 10": 0.30; 'worked': 0.30; 'fri,': 0.30; 'function': 0.30; 'error': 0.30; 'figure': 0.30; 'code': 0.31; '(and': 0.32; 'url:python': 0.32; 'file': 0.32; 'more,': 0.32; "skip:' 20": 0.32; 'print': 0.32; '(a)': 0.33; "aren't": 0.33; 'docs': 0.33; 'quotes': 0.33; 'tech': 0.33; 'handle': 0.33; 'another': 0.33; 'received:google.com': 0.34; 'or,': 0.34; 'list': 0.35; 'said,': 0.35; 'so,': 0.35; 'open': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'something': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; "didn't": 0.36; "i'll": 0.36; 'test': 0.36; 'should': 0.36; 'itself': 0.37; 'summer': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'url:docs': 0.38; 'header:Received:5': 0.40; 'your': 0.60; 'company': 0.60; 'most': 0.61; "you'll": 0.62; 'back': 0.62; 'more': 0.63; 'covers': 0.65; 'obvious': 0.71; '"too': 0.84; 'escaping': 0.84; 'seldom': 0.84; 'sender:addr:chris': 0.84; 'summer,': 0.84; 'mistakes': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=UqLylgJDAqoQzgy0kTHBpasHoIcr8AzGfZ50ws51HRU=; b=QMURRpF27BSfmSRVvfmBNdEiSYxF0wrI5wZ01N92E7Vom5IkbiFcenmkKM9J5zSzdd eyP9cvWe2nIPTH0NVb3dsiJ/h4eWTQgBzUoQKC3Q059POGAiwi5ihqO5V2y156Ql899X Ntku2GqATZS5/EjDoMRzO9MlFoCXg/cza4VJ0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding:x-gm-message-state; bh=UqLylgJDAqoQzgy0kTHBpasHoIcr8AzGfZ50ws51HRU=; b=GLbixU7Ieff936TYC45q1o28Yi7Q0hI2fg3an/FFwnPgFXmy+qOX5knGWwLUaSU6W0 y4sOQwf9OwFJRHcgrWr58VI8YvURjEialWW1nA3KjvZiPa46n58ZxkxHHWAG86Xx55cQ jZkIVB9a1azSk3Ftz7KuByhMiygwVIPZl/BrxFMe9iSY4DhlkM1GhceLVrKWf3lOe9tZ gHsYxMmA6lq38Ze6L09B3TQfdj6lixcPc7iyaXpHjsKxhNDADEEV4J3WBkcpoeoMNqvK pcvhsxsA7NbQ3PDj4+aFyKYa8pswFyM/yWZ8TNK5Ru/TD6G/IK+5nf3wIT4+cI35Bdod /A8A== MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <0b598d7f-7137-47cc-8eed-80ab450536c5@googlegroups.com> References: <0b598d7f-7137-47cc-8eed-80ab450536c5@googlegroups.com> Date: Fri, 14 Sep 2012 21:02:22 -0700 X-Google-Sender-Auth: 2Ud9Xz6wdLXz7BPMQApv0PO86lE Subject: Re: subprocess call is not waiting. From: Chris Rebert To: paulstaten@gmail.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQk3ezVplts81MtBW/yP0m8sMOwmfGBd6Xib69yCni2DO2Ss6Jc/i0Y14NZMNvKmQ6Czj5ab Cc: python-list@python.org 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: 78 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347681745 news.xs4all.nl 6931 [2001:888:2000:d::a6]:50242 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29205 On Fri, Sep 14, 2012 at 5:22 AM, wrote: > os.system worked fine, and I found something in another section of code t= hat was causing the "Too many open errors." (I was fooled, because output f= rom subprocess call didn't seem to be coming out until the open files error= . > > I'll go back and play with subprocess.call more, since os.system works. T= hat's interesting about using shlex at run time. Is that just for the sake = of computational cost? No, like I said, you'll also get incorrect results. shlex isn't magic. If the exact command line it's given wouldn't work in the shell, then it won't magically fix things. Many (most?) dynamic invocations of shlex.split() are naive and flawed: >>> import shlex >>> filename =3D "my summer vacation.txt" >>> # the following error is less obvious when the command is more complex >>> # (and when the filename isn't hardcoded) >>> cmd =3D "cat " + filename >>> shlex.split(cmd) ['cat', 'my', 'summer', 'vacation.txt'] >>> # that's wrong; the entire filename should be a single list element Equivalent bash error: chris@mbp ~ $ cat my summer vacation.txt cat: my: No such file or directory cat: summer: No such file or directory cat: vacation.txt: No such file or directory The right way, in bash: chris@mbp ~ $ cat my\ summer\ vacation.txt Last summer, I interned at a tech company and... chris@mbp ~ $ cat 'my summer vacation.txt' Last summer, I interned at a tech company and=E2=80=A6 And indeed, shlex will get that right too: >>> shlex.split("cat my\ summer\ vacation.txt") ['cat', 'my summer vacation.txt'] >>> shlex.split("cat 'my summer vacation.txt'") ['cat', 'my summer vacation.txt'] BUT that presumes that your filenames are already pre-quoted or have had backslashes added, which very seldom is the case in reality. So, you can either find an escaping function and hope you never forget to invoke it (cf. SQL injection), or you can figure out the general tokenization and let `subprocess` handle the rest (cf. prepared statements): >>> split('cat examplesimplefilename') ['cat', 'examplesimplefilename'] >>> # Therefore=E2=80=A6 >>> def do_cat(filename): ... cmd =3D ['cat', filename] # less trivial cases would be more intere= sting ... call(cmd) ... >>> filename =3D "my summer vacation.txt" >>> # remember that those quotes are Python literal syntax and aren't in th= e string itself >>> print filename my summer vacation.txt >>> do_cat(filename) Last summer, I interned at a tech company and=E2=80=A6 >>> Generally, use (a) deliberately simple test filename(s) with shlex, then take the resulting list and replace the filename(s) with (a) variable(s). Or, just figure out the tokenization without recourse to shlex; it's not difficult in most cases! The Note in the Popen docs covers some common tokenization mistakes people = make: http://docs.python.org/library/subprocess.html#subprocess.Popen Cheers, Chris