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

2024/11/17 7:19:04

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?

Answer

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://en.xdnf.cn/q/71574.html

Related Q&A

cant import django model into celery task

i have the following task:from __future__ import absolute_importfrom myproject.celery import appfrom myapp.models import Entity@app.task def add(entity_id):entity = Entity.objects.get(pk=entity_id)retu…

Running unit tests with Nose inside a Python environment such as Autodesk Maya?

Id like to start creating unit tests for my Maya scripts. These scripts must be run inside the Maya environment and rely on the maya.cmds module namespace.How can I run Nose tests from inside a runnin…

Python Newline \n not working in jupyter notebooks

Im trying to display the tuples of a postgreSQL table neatly in my Jupyter Notebook, but the newline \n escape character doesnt seem to work here (it works for my python scripts w/ same code outside of…

Dynamically calling functions - Python

I have a list of functions... e.g.def filter_bunnies(pets): ...def filter_turtles(pets): ...def filter_narwhals(pets): ...Is there a way to call these functions by using a string representing their nam…

py2exe windows service problem

I have successfully converted my python project to a service. When using the usual options of install and start/stop, everything works correctly. However, I wish to compile the project using py2exe, …

Draw a cumulative chart from a pandas dataframe?

I have a dataframe as follows:df = pd.DataFrame({cost_saving: [10, 10, 20, 40, 60, 60],id: [a, b, c, d, e, f]})How can I draw a cumulative chart of the savings?Im thinking of a line chart with number …

Sort a complex Python dictionary by just one of its values

I am writing a little optimization tool for purchasing stamps at the post office.In the process I am using a dictionary, which I am sorting according to what I learned in this other "famous" …

How to modularize a Python application

Ive got a number of scripts that use common definitions. How do I split them in multiple files? Furthermore, the application can not be installed in any way in my scenario; it must be possible to have…

Solving for quartile and decile using Python

Is there a library for Python 2.7 that can solve quartiles and deciles. It seems that numpy doesnt have any functions for it. Can you give me a link if there are any. Thanks in advance! :D

Predicted values of each fold in K-Fold Cross Validation in sklearn

I have performed 10-fold cross validation on a dataset that I have using python sklearn, result = cross_val_score(best_svr, X, y, cv=10, scoring=r2) print(result.mean())I have been able to get the mean…