6

I'm using Python and Envoy. I need to delete all files in a directory. Apart from some files, the directory is empty. In a terminal this would be:

rm /tmp/my_silly_directory/*

Common sense dictates that in envoy, this translates into:

r = envoy.run('rm /tmp/my_silly_directory/*')

However:

r.std_err -> "rm: cannot remove `/tmp/my_silly_directory/*': No such file or directory"

Naturally there are alternatives to using envoy in this case, I am simply wondering why it doesn't work.

Any clues?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Jouke Waleson
  • 517
  • 1
  • 3
  • 14

1 Answers1

5

On UNIX, it's up to the shell to interpret wildcards like *. If you execute a program and pass an argument with * in it directly to the program -- which is presumably what's being done here -- then you'll get an error like you're seeing. rm just assumes the * is a file name, and indeed, it's actually possible to create such a file.

One solution could be to execute the shell and let it execute your command on your behalf, something like

r = envoy.run('sh -c "rm /tmp/my_silly_directory/*"')

The shell will interpret the * before invoking rm.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • This sounds perfectly right, and would probably work with subprocess. However, in envoy, the only call that works is `envoy.run([["sh", "-c", "rm /tmp/my_silly_directory/*"]])` for some reason. So much for a simpler `subprocess` ;) – Jouke Waleson Jul 30 '12 at 13:42
  • couldn't get this to work with subprocess, by the way. Same error as original question. – Nate Sep 04 '13 at 16:56
  • @Nate: [`shutil.rmtree('/tmp/my_silly_directory/')`](http://stackoverflow.com/questions/9106350/how-to-use-wildcards-with-envoy?lq=1#comment15551439_9109127) – jfs Dec 05 '13 at 18:33