Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to get a list of all files and folders with there sizes (upto 1st level, i.e. file and folder names with there sizes in current directory) emailed to me via an python script. I got the script working but using ls command as below only sends file-sizes and not folder sizes ?

p=subprocess.Popen(['ls', '-1ahs', '/media/ExternalHd/myfolder'], stdout=subprocess.PIPE)
output =
share|improve this question
Try the du command instead of ls. – Pedro Romano Nov 19 '12 at 16:03
@PedroRomano did that but it lists folders only and not filesizes ? Maybe a hint towards du usage to list file and folder sizes would help... – DeepeshAgarwal Nov 20 '12 at 7:10
Have you tried du -s *? – Pedro Romano Nov 20 '12 at 19:42
@PedroRomano using du -sh /media/ExternalHd/myfolder/* works on terminal, but using p=subprocess.Popen(['du', '-sh', '/media/ExternalHd/myfolder/*'], stdout=subprocess.PIPE) in a python script shows error du: cannot access /media/ExternalHd/myfolder/*: No such file or directory – DeepeshAgarwal Nov 21 '12 at 8:21
up vote 1 down vote accepted

You need a shell to expand "*", Popen's not going to it for you. So if you really want to use Popen, then

p=subprocess.Popen(['sh', '-c', 'du', '-sh', '/media/ExternalHd/myfolder/*'], stdout=subprocess.PIPE)
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .