Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '>>>>': 0.09; 'admit': 0.09; 'module?': 0.09; 'shortcut': 0.09; 'subprocess': 0.09; 'err': 0.16; 'shed': 0.16; 'subject:subprocess': 0.16; 'trash': 0.16; 'x),': 0.16; '\xc2\xa0i': 0.16; 'cc:addr:python-list': 0.16; 'subject:question': 0.17; 'wrote:': 0.18; 'slightly': 0.19; 'this?': 0.19; 'cc:no real name:2**0': 0.20; 'cheers,': 0.20; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'received:209.85.212.46': 0.23; 'received:mail- vw0-f46.google.com': 0.23; '(on': 0.23; 'cc:2**0': 0.24; 'shell': 0.24; "i'm": 0.26; 'import': 0.27; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'skip:\xc2 20': 0.30; 'sun,': 0.30; 'chris': 0.30; 'url:library': 0.31; 'anyone': 0.31; "i've": 0.31; 'does': 0.32; 'wanting': 0.32; 'wondering': 0.32; "can't": 0.32; 'received:209.85.212': 0.34; 'light': 0.35; 'test': 0.35; 'url:python': 0.36; 'question': 0.36; 'security.': 0.37; 'but': 0.37; 'reasons': 0.37; 'received:google.com': 0.37; 'another': 0.37; 'could': 0.37; 'using': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'skip:o 20': 0.38; 'url:docs': 0.39; 'url:org': 0.39; 'why': 0.39; "it's": 0.40; 'received:209': 0.40; 'might': 0.40; '2011': 0.61; 'your': 0.61; 'home': 0.65; '11,': 0.68; 'yourself': 0.69; 'sender:addr:chris': 0.84; 'url:os': 0.84; 'url:rebertia': 0.84 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=aBW36rpdMW8lyU3i8JwH02TWsI+MNKtsvXqrgzmufRw=; b=E7zGpCvReTQNvVV/91nyy/pF8lwGhfZ3qx9Gajp94wSytchA4G3BYrOYADUlBcvXo2 no2yYT43ME5CAAcLJ8mw9XuBEWJgN3wdC6GLa5pWNDEIGSN1Aw+zRYCZ12Ela4NvbdJy g6MRYYEP3OV5V4ey1bAkzGMUsuZML/yOHJoGk= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> References: <20111212043933.BCJ1L.23648.root@cdptpa-web38-z02.mail.rr.com> Date: Sun, 11 Dec 2011 22:02:23 -0800 X-Google-Sender-Auth: D47CWSLNaxJd8fp3gx0s5_TqCQI Subject: Re: subprocess question From: Chris Rebert To: jyoung79@kc.rr.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323669745 news.xs4all.nl 6870 [2001:888:2000:d::a6]:53853 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17014 On Sun, Dec 11, 2011 at 8:39 PM, wrote: > Wondering if anyone could shed some light on the subprocess module? =C2= =A0I'll admit I'm not that great at the shell. > > If I was wanting to get the size of the trash (on OS X), I could use: > >>>> os.system('du -sh ~/.Trash/') > =C2=A011M =C2=A0 =C2=A0/Users/jay/.Trash/ > 0 > > Which gives me what I want. =C2=A0However, I've been reading that it's be= tter to use subprocess. =C2=A0I did a test like so, but is this a good way = to do this? > >>>> import subprocess >>>> p =3D subprocess.Popen(['du', '-sh'], cwd=3D'/Users/jay/.Trash/', stdo= ut=3Dsubprocess.PIPE) >>>> out, err =3D p.communicate() >>>> out > ' 11M\t.\n' You might prefer to use subprocess.check_output(); it slightly simplifies your code: http://docs.python.org/library/subprocess.html#subprocess.check_output > And another question - why can't I use the tilde as a shortcut to the hom= e directory? Because ~ is interpolated by the shell and `subprocess` does not use the shell by default for reasons that include efficiency and security. You can expand ~ yourself using os.path.expanduser(): http://docs.python.org/library/os.path.html#os.path.expanduser Alternatively, you can opt to use the shell by passing shell=3DTrue as an argument. Cheers, Chris -- http://rebertia.com