subprocess question



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:
11M /Users/jay/.Trash/

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'

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/", line 672, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 1202, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory: '~/.Trash/'

Thanks for looking at my questions.



Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question