Skip to content

Copy between different filesystems #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Nintorac opened this issue Nov 25, 2024 · 1 comment
Open

Copy between different filesystems #315

Nintorac opened this issue Nov 25, 2024 · 1 comment

Comments

@Nintorac
Copy link

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!

@AllanChain
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants