re. this comment #30195 (review) and needing to use the full path again, I think it is that rename(...) returns a new Path instance, which is then the 'correct' one to use going forwards for further changes you want to happen on-disk (alas Python has no 'use of moved value' concept like Rust et al. that could've pointed this out).
I think the way to think of this is that a Path is just a path, not a file-handle, so you can have a Path instance that doesn't exist, because for all Python knows you might be wanting to create it -- so .rename(...) on a Path that doesn't exist changes the path instance but of-course can't change the actual filesystem because it doesn't exist.
>>> from pathlib import Path
>>> # Make a Path and rename it to add `".copy"` on the end:
>>> p = Path("README.md")
>>> p.exists()
True
>>> q = p.rename("README.md.copy") # capture the return value in `q`
>>>
>>> # We still have the Path instance called `p`...
>>> p
PosixPath('README.md')
>>> # ...but it doesn't exist anymore (becuase we just renamed it)
>>> p.exists()
False
>>> # `.rename` returned a new Path instance, which *does* exist
>>> q
PosixPath('README.md.copy')
>>> q.exists()
True
>>>
>>> # Just to prove the rename happened:
>>> [item.name for item in Path(".").iterdir() if "md" in item.name]
['README.md.copy']
>>>
>>> # Rename again to move it back to the original name...
>>> r = q.rename("README.md")
>>> r
PosixPath('README.md')
>>> r.exists()
True
>>> # Because `p` and `r` now have the same name, the original `p` also 'exists' again:
>>> p.exists(), q.exists(), r.exists()
(True, False, True)
So:
# Renaming the block file to induce unsuccessful block read
blk_dat = (self.nodes[0].blocks_path / "blk00000.dat")
assert blk_dat.exists()
blk_dat_moved = blk_dat.rename("blk00000.dat.moved")
assert not blk_dat.exists()
# listsinceblock(nodes1_last_blockhash) should now fail as blocks are not accessible
assert_raises_rpc_error(-32603, "Can't read block from disk",
self.nodes[0].listsinceblock, nodes1_last_blockhash)
# Restoring block file
blk_dat_moved.rename("blk00000.dat")
assert blk_dat.exists()
Also suggest naming change to "moved" or "backup" instead of "copy" as you're not copying?
And additional asserts to check that the files exist and don't when expected (maybe overkill, but prevents unexpected pass/fails for some other reason than you renaming the file)