Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os.remove() does not remove files in a sub-directory #95758

Closed
pjfarley3 opened this issue Aug 7, 2022 · 2 comments
Closed

os.remove() does not remove files in a sub-directory #95758

pjfarley3 opened this issue Aug 7, 2022 · 2 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@pjfarley3
Copy link

pjfarley3 commented Aug 7, 2022

Bug report

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)

  • Operating system and architecture:

  • z/OS V2.4 (IBM Zxplore system, available free online at https://ibmzxplore.influitive.com/)

  • Ubuntu 20.04 LTS under Windows WSL

  • Ubuntu 20.04 LTS under VirtualBox 6.1.30 on Windows 10 64 Pro

uname and python version outputs follow.

z/OS Unix:
/z/zxxxxx > uname -a
OS/390 S0W1 27.00 04 3906
/z/zxxxxx > python3 -V
Python 3.9.2

WSL Ubuntu:
userid@DESKTOP:~$ uname -a
Linux DESKTOP 4.4.0-19041-Microsoft #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 x86_64 x86_64 x86_64 GNU/Linux
userid@DESKTOP:~$ python3 -V
Python 3.8.10

VirtualBox Ubuntu:
userid@Ubuntu20:~$ uname -a
Linux Ubuntu20 5.15.0-43-generic #46~20.04.1-Ubuntu SMP Thu Jul 14 15:20:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
userid@Ubuntu20:~$ python3 -V
Python 3.8.10

Attaching shell and python scripts here as *.txt files, rename to execute:

testosfnc.sh.txt

testosfnc.py.txt

Logs from each tested environment:

z/OS Unix:
testosfnc.zos.log

WSL Ubuntu:
testosfnc.wslubu.log

VirtualBox Ubuntu:
testosfnc.vbxubu.log

Please also CC me at pjfarley3 at earthlink dot net for any questions or updates.

@pjfarley3 pjfarley3 added the type-bug An unexpected behavior, bug, or error label Aug 7, 2022
@eryksun
Copy link
Contributor

eryksun commented Aug 7, 2022

os.remove() does not support shell wildcard characters such as *. In a POSIX filename, * is just a name character. For example:

>>> open('*', 'w').close()
>>> os.path.isfile('*')
True
>>> os.remove('*')
>>> os.path.exists('*')
False

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.

@pjfarley3
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants