Hello everyone. I am new here and I am looking for help with an issue that I am having with copying files using pathlib. I’m running python3.14 on Linux Mint.
When I try to use the .copy method on pathlib’s Path class to copy a file, it raises an OSError[22]. So far it looks like this happens when trying to copy a non-empty file. Here is the script that I am using to reproduce the error.
#!/usr/bin/env python3
from pathlib import Path
SRC = Path(__file__).parent / 'test1.txt'
DEST = Path(__file__).parent / 'test2.txt'
SRC.copy(DEST)
The contents of the file test1.txt are below:
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
And here is the traceback that I get from running the code.
Traceback (most recent call last):
File "/home/robert/src/Sandbox/Test/./copy.py", line 8, in <module>
SRC.copy(DEST)
File "/usr/lib/python3.14/pathlib/__init__.py", line 1103, in copy
target._copy_from(self, **kwargs)
File "/usr/lib/python3.14/pathlib/__init__.py", line 1134, in _copy_from
self._copy_from_file(source, preserve_metadata)
File "/usr/lib/python3.14/pathlib/__init__.py", line 1140, in _copy_from_file
copyfileobj(source_f, target_f)
File "/usr/lib/python3.14/pathlib/_os.py", line 160, in copyfileobj
raise err
File "/usr/lib/python3.14/pathlib/_os.py", line 148, in copyfileobj
raise err
File "/usr/lib/python3.14/pathlib/_os.py", line 144, in copyfileobj
_copy_file_range(source_fd, target_fd)
File "/usr/lib/python3.14/pathlib/_os.py", line 78, in _copy_file_range
sent = os.copy_file_range(source_fd, target_fd, blocksize,
OSError: [Errno 22] Invalid argument: '/home/robert/src/Sandbox/Test/test1.txt' -> '/home/robert/src/Sandbox/Test/test2.txt'
It seems to work fine when I use shutil.copy2 instead. Does anyone know why this might be occurring?
Cheers,
-Robert