add unit test for writing to already open file
- [ ] closes
- [x] tests added / passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

closes #21577 
The added unit test will pass for `0.23.0`, fails for `0.23.1`, passes for `master`.