You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
os.remove() does not remove files in a sub-directory #95758
Using os.remove() to remove multiple files in a subdirectory using an absolute path does not work in multiple linux environments, including z/OS Unix, Ubuntu Linux 20.04 under Windows WSL, and Ubuntu Linux 20.04 in VIrtualBox under Windows.
In each environment, the attempt was made to accomplish a multiple-file remove from a directory as follows:
os.remove("/abs/path/to/currdir/contest/*")
Where "/abs/path/to/currdir" is returned from os.getcwd() and path separators are added via os.sep.
Also tested was using subprocess.run to execute an "rm" command (with no flags) to remove the same files followed by an "rmdir" command to remove the directory, and both of those also fail to remove the files as requested and therefore the directory since it is not empty.
In all cases, performing the equivalent "rm" and "rmdir" commands from a shell prompt or from a shell script directly (rather than from inside a python program) succeed as expected.
Shell script and python script demonstrating the issue are attached, along with logs of output from the three environments tested.
My environments
CPython versions tested on:
python 3.9.5 on z/OS Unix
python 3.8.10 on Ubuntu 20.04 (WSL and VirtualBox)
You can use shutil.rmtree() to recursively remove a directory and its contents. You can use glob.glob() or pathlib.Path.glob() to list files in a way that supports shell-like wildcards.
Thank you for the clarification of os.remove functionality and the reference to shutil.rmtree(). If I can I will submit a documentation pull to make it clearer that os.remove() does not support wildcard file globbing for future first-time users of the function. My git-fu is negligible and very, very new, so it may take me a long while to figure out how to actually submit one.
By extension, I presume that "no wildcard support" is true for os.removedirs() as well, correct?
Activity
eryksun commentedon Aug 7, 2022
os.remove()does not support shell wildcard characters such as*. In a POSIX filename,*is just a name character. For example:You can use
shutil.rmtree()to recursively remove a directory and its contents. You can useglob.glob()or pathlib.Path.glob() to list files in a way that supports shell-like wildcards.pjfarley3 commentedon Aug 7, 2022
Thank you for the clarification of os.remove functionality and the reference to shutil.rmtree(). If I can I will submit a documentation pull to make it clearer that os.remove() does not support wildcard file globbing for future first-time users of the function. My git-fu is negligible and very, very new, so it may take me a long while to figure out how to actually submit one.
By extension, I presume that "no wildcard support" is true for os.removedirs() as well, correct?