The terminal expands the * for you. To tell subprocess to do that:
p=subprocess.Popen('du -sh /tmp/*', shell=True)
Or you could use the glob module to expand the * yourself, if you needed more control
Вопрос
With reference to my question at SuperUser, I am facing a puzzling situation where using du -sh /media/ExternalHd/myfolder/*
works as expected from 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
Решение
The terminal expands the * for you. To tell subprocess to do that:
p=subprocess.Popen('du -sh /tmp/*', shell=True)
Or you could use the glob module to expand the * yourself, if you needed more control
Другие советы
You should add the parameter shell=True
to your subprocess.Popen func. So that you can invoke the shell and use environment variables, file globs etc.
p = subprocess.Popen(['du', '-sh', '/media/ExternalHd/myfolder/*'], stdout=subprocess.PIPE, shell=True)
However, you should avoid using shell=True
because of the security hazards, see the warning in python subprocess module docs. For a small script like this, maybe it doesn't create a problem, but keep in mind ;)
For further details, see this answer to another stackoverflow question.