Skip to content

os.remove missing exception type #121509

Open

Description

@prairir

Documentation

The documentation for os.remove states On Windows, attempting to remove a file that is in use causes an exception to be raised; but doesnt mention the exception type being returned. The exception type is PermissionError. It should be documented

Linked PRs

Activity

  1. added
    docs
    on Jul 8, 2024
  2. eryksun commented on Jul 8, 2024

    @eryksun
    Contributor

    This wording is slightly off. The docs should say that trying to remove an in-use file "may" cause an exception to be raised, while in some cases it may not. For example:

    >>> fd = os.open('test', os.O_CREAT|os.O_WRONLY|os.O_TEMPORARY)
    >>> os.write(fd, b'spam')
    4
    >>> os.listdir()
    ['test']
    >>> os.remove('test')
    >>> os.listdir()
    []

    In this case, the flag O_TEMPORARY opens the file with DELETE access and FILE_SHARE_DELETE share mode. Thus os.remove() is allowed to reopen the file with DELETE access.

    OTOH, opening via builtin open() doesn't share delete access, so trying to reopen the file with delete access fails with the Windows error code ERROR_SHARING_VIOLATION (32), and Python raises PermissionError.

  3. added a commit that references this issue on Nov 11, 2026
  4. added a commit that references this issue on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions