subprocess question

jyoung79 at kc.rr.com jyoung79 at kc.rr.com
Mon Dec 12 09:49:06 EST 2011


>>>>> 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 

Thank you very much, Chris and Andrew, for your help.  I appreciate it!  :-)

Jay



More information about the Python-list mailing list