How do I delete a file or folder in Python?
os.remove()removes a file.
os.rmdir()removes an empty directory.
shutil.rmtree()deletes a directory and all its contents.
Path objects from the Python 3.4+
pathlib module also expose these instance methods:
pathlib.Path.unlink()removes a file or symbolic link.
pathlib.Path.rmdir()removes an empty directory.
I have seen the file system watcher fail in production and test environments. I now consider it a convenience, but I do not consider it reliable. My pattern has been to watch for changes with the files system watcher, but poll occasionally to catch missing file changes.
Edit: If you have a UI, you can also give your user the ability to "refresh" for changes instead of polling. I would combine this with a file system watcher.
You should check out Apache's commons-io. It has a FileUtils class that will do what you want.
No, I don't know one.
If you want to retain the original directory for some reason (ACLs, &c.), and instead really want to empty it, then you can do the following:
del /q destination* for /d %x in (destination*) do @rd /s /q "%x"
This first removes all files from the directory, and then recursively removes all nested directories, but overall keeping the top-level directory as it is (except for its contents).
Note that within a batch file you need to double the
% within the
del /q destination* for /d %%x in (destination*) do @rd /s /q "%%x"
Simplest way would be to use FileUtils.deleteDirectory from the Apache Commons IO library.
File dir = new File("root path"); FileUtils.deleteDirectory(dir);
Bear in mind this will also delete the containing directory.
Add this line in gradle file to have Apache