subprocess question
jyoung79 at kc.rr.com
jyoung79 at kc.rr.com
Sun Dec 11 23:39:33 EST 2011
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
More information about the Python-list
mailing list