在特使中使用rm *(通配符):无此类文件或目录 [英] Using rm * (wildcard) in envoy: No such file or directory

查看:205
本文介绍了在特使中使用rm *(通配符):无此类文件或目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python和Envoy.我需要删除目录中的所有文件.除某些文件外,该目录为空.在终端中,这将是:

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/*')

但是:

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

在这种情况下,自然有使用envoy的替代方法,我只是想知道为什么它不起作用.

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

有任何线索吗?

推荐答案

在UNIX上,由外壳程序解释诸如*之类的通配符.如果执行程序并将参数中带有*的参数直接传递给程序(这大概是在此处执行的操作),那么您将看到错误,就像看到的一样. rm仅假定*是文件名,实际上,实际上可以创建这样的文件.

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.

一种解决方案可能是执行shell,并让它代表您执行命令,例如

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/*"')

shell将在调用rm之前解释*.

The shell will interpret the * before invoking rm.

这篇关于在特使中使用rm *(通配符):无此类文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆