universal_pathlib icon indicating copy to clipboard operation
universal_pathlib copied to clipboard

Copy between different filesystems

Open Nintorac opened this issue 1 year ago • 2 comments

Hi, great project :)

I am trying to copy between filesystems, I see that #227 exists, that will be amazing when it comes!

In the meantime I would still like to copy between filesystems, I wrote some code like this,

source_uri = UPath('some_file')
target_uri = UPath('s3://test/some_file')
with (source_uri.open('rb') as source,
     target_uri.open('wb') as target):
    target.write(source)

but get the error TypeError: a bytes-like object is required, not '_io.BufferedReader..since target.write seemingly should accept a readable buffer I am not sure why the error here.

The workaround I have come up with looks like this, feel kind of wrong to implement it in python loop though.

with (source_uri.open('rb') as source,
        target_uri.open('wb') as target):
    while x:=source.read(target_uri.fs.blocksize):
        target.write(x)

Is there something obviously wrong with my initial implementation or is there a better approach for the workaround?

Cheers!

Nintorac avatar Nov 25 '24 09:11 Nintorac

Your initial implementation is wrong because you can only write bytes instead of file objects using write(). You should explicitly read bytes from the source and write them to the target, just as what your workaround does.

AllanChain avatar Mar 05 '25 12:03 AllanChain

This is now supported via


source_uri = UPath('some_file')
target_uri = UPath('s3://test/some_file')

source_uri.copy(target_uri)

Might not run at optimal speed but that can be worked out in a future release.

ap-- avatar Oct 03 '25 22:10 ap--