0

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

Community
  • 1
  • 1
Stacked
  • 841
  • 2
  • 12
  • 23

2 Answers2

2

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

pixelbeat
  • 30,615
  • 9
  • 51
  • 60
0

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.

Community
  • 1
  • 1
Serdar Dalgic
  • 1,322
  • 8
  • 17