Deprecate os.removedirs() and os.renames()
It seems to me that os.removedirs() and os.renames() was added just for symmetry with os.makedirs(). All three functions have similar structure and was added in the same commit. Seems they were initially code examples of using some os.path and os functions. Unlike to quite popular os.makedirs(), os.removedirs() and os.renames() are not used in the stdlib and rarely used in third party code. os.removedirs() is considered as an opposite to os.makedirs(), and os.renames() is a combination of os.makedirs(), os.rename() and os.removedirs(). The problems with them are: 1. They do not remove directory if any file or other subdirectory is left. They just stop removing and return success. ZTo the user it looks like they do not work as expected, but he need to test the existence of directory explicitly to check this. 2. They can remove more than expected. If the parent directory was empty before calling os.makedirs(), the following os.removedirs() will remove not just the newly created directories, but the parent directory, and its parent if it contained a single directory, and so on. os.removedirs() is not an opposite to os.makedirs(). It can remove less or more, and you have no control on how much it will remove. It is better to use shutil.rmtree(). os.renames() correspondingly can be replaced by os.rename() or shutil.move(), with possible addition of os.makedirs() and shutil.rmtree() if needed. I propose to deprecate these functions and remove them in future Python versions.
Serhiy Storchaka wrote:
I propose to deprecate these functions and remove them in future Python versions.
+1, assuming the deprecation lasts for at least two versions and the available alternatives are explicitly mentioned in the What's New entry (for both the version they're initially deprecated in and the one they become removed in). Although, I suspect that the deprecation may need to be longer than two versions from possible breakage in older libraries or legacy code. They don't seem at all common compared to shutil.move() and shutil.rmtree(), but I do vaguely recall seeing some usage of os.renames() and os.removedirs() in third party code. On Thu, May 7, 2020 at 4:06 PM Serhiy Storchaka <storchaka@gmail.com> wrote:
It seems to me that os.removedirs() and os.renames() was added just for symmetry with os.makedirs(). All three functions have similar structure and was added in the same commit. Seems they were initially code examples of using some os.path and os functions.
Unlike to quite popular os.makedirs(), os.removedirs() and os.renames() are not used in the stdlib and rarely used in third party code. os.removedirs() is considered as an opposite to os.makedirs(), and os.renames() is a combination of os.makedirs(), os.rename() and os.removedirs(). The problems with them are:
1. They do not remove directory if any file or other subdirectory is left. They just stop removing and return success. ZTo the user it looks like they do not work as expected, but he need to test the existence of directory explicitly to check this.
2. They can remove more than expected. If the parent directory was empty before calling os.makedirs(), the following os.removedirs() will remove not just the newly created directories, but the parent directory, and its parent if it contained a single directory, and so on.
os.removedirs() is not an opposite to os.makedirs(). It can remove less or more, and you have no control on how much it will remove. It is better to use shutil.rmtree().
os.renames() correspondingly can be replaced by os.rename() or shutil.move(), with possible addition of os.makedirs() and shutil.rmtree() if needed.
I propose to deprecate these functions and remove them in future Python versions. _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/MWUFGKT4... Code of Conduct: http://python.org/psf/codeofconduct/
participants (2)
-
Kyle Stanley
-
Serhiy Storchaka