Using rm * (wildcard) in envoy: No such file or directory

梦想的初衷 提交于 2019-12-10 04:24:39

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/11722318/using-rm-wildcard-in-envoy-no-such-file-or-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!