Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17014 > unrolled thread
| Started by | Chris Rebert <clp2@rebertia.com> |
|---|---|
| First post | 2011-12-11 22:02 -0800 |
| Last post | 2011-12-12 15:16 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: subprocess question Chris Rebert <clp2@rebertia.com> - 2011-12-11 22:02 -0800
Re: subprocess question Nobody <nobody@nowhere.com> - 2011-12-12 15:16 +0000
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-12-11 22:02 -0800 |
| Subject | Re: subprocess question |
| Message-ID | <mailman.3531.1323669745.27778.python-list@python.org> |
On Sun, Dec 11, 2011 at 8:39 PM, <jyoung79@kc.rr.com> wrote:
> Wondering if anyone could shed some light on the subprocess module? I'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/')
> 11M /Users/jay/.Trash/
> 0
>
> Which gives me what I want. However, I've been reading that it's better to use subprocess. I did a test like so, but is this a good way to do this?
>
>>>> import subprocess
>>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE)
>>>> out, err = 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 home 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=True as
an argument.
Cheers,
Chris
--
http://rebertia.com
[toc] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2011-12-12 15:16 +0000 |
| Message-ID | <pan.2011.12.12.15.16.54.597000@nowhere.com> |
| In reply to | #17014 |
On Sun, 11 Dec 2011 22:02:23 -0800, Chris Rebert wrote: >>>>> p = subprocess.Popen(['du', '-sh'], cwd='/Users/jay/.Trash/', stdout=subprocess.PIPE) > Alternatively, you can opt to use the shell by passing shell=True as > an argument. Except that the OP is talking about a directory passed to the cwd= parameter, rather than as part of the command, and shell= doesn't affect that. But even if the directory was part of the command, just setting shell=True won't work. On Unix (including MacOSX) the call: subprocess.Popen(['du', '-sh'], shell=True) is equivalent to: subprocess.Popen(['/bin/sh', '-c', 'du', '-sh'], shell=False) This will result in the shell executing "du" with no arguments. The variable expansion "$1" will evaluate to "-sh", but that's meaningless as "$1" doesn't occur in the argument to "-c". The combination of using a list for the "args" parameter along with shell=True is rarely useful on Unix. And using a string for the "args" parameter introduces all of the associated reliability and security issues. The situation is different on Windows, where list/string and shell= are orthogonal. A list is always converted to a string according to the rules by which MSVCRT parses the command line into argv[]. Then, if shell=True, "cmd.exe /c " is prepended to the string (actually, the value of the COMSPEC environment variable is used in place of cmd.exe if it is defined). Setting shell=True allows the "program" to be any file with a registered extension, rather than just an executable.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web