`remove_path` method behaviour in `pebble`
See original GitHub issueThe remove_path
method in pebble
has an optional argument recursive: bool = False
:
def remove_path(self, path: str, *, recursive: bool = False):
Let’s say that you need to delete a file from a container’s directory. But it’s possible that the file you trying to delete it is not yet in the directory.
If you have:
container.remove_path(path)
You will get a PathError
exception since the file does not exist. This makes sense, it is behaviourally similar to rm <file>
:
➜ rm pepe.txt
rm: cannot remove 'pepe.txt': No such file or directory
But there is no explicit option that behaves similar to rm -f <file>
(do nothing if the files is non-existent):
➜ rm -f pepe.txt
➜
Anyway you can achieve that behaviour by using recursive=True
container.remove_path(path, recursive=True)
recursive=True
behaves in 2 ways:
- If
path
is a directory recursively deletes it and everything under it. - If the
path
is a file, delete the file and do nothing if the file is non-existent. Behaviourally similar torm -rf <file|dir>
Using recursive=True
when trying to delete a file and do nothing if file is non-existent (rm -f <file>
) is a little bit confusing.
Shouldn’t we try to mimic the rm behaviour where we have: -r
for recursive and -f
for “ignore nonexistent files and arguments, never prompt”?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:7 (7 by maintainers)
Top GitHub Comments
I suspect that a lot of people using the lib would just pass -f, and create their own problems detecting typos.
I like the simplicity of having the library work one way, with my preferred way being to remove a path if it exists, and log a warning if it does not. (The warning gives typo makers a chance to catch their error.)
Thank you for filing this!