Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17008
| Date | 2011-12-12 04:39 +0000 |
|---|---|
| From | <jyoung79@kc.rr.com> |
| Subject | subprocess question |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3527.1323664783.27778.python-list@python.org> (permalink) |
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'
>>> err
>>>
And another question - why can't I use the tilde as a shortcut to the home directory?
>>> p = subprocess.Popen(['du', '-sh'], cwd='~/.Trash/', stdout=subprocess.PIPE)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 672, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1202, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory: '~/.Trash/'
Thanks for looking at my questions.
Jay
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
subprocess question <jyoung79@kc.rr.com> - 2011-12-12 04:39 +0000
csiph-web