1

I'm trying to move a file to a pen-drive using linux raspberry, python3, os and shutil, but i just receive errors.

When i try to save in a another directory (not usb), the code works great

import shutil
import os

original = '/home/guardian/shared/TestPath/New/'
new = '/media/guardian/GUARDIANT1/New/'

for dir, sub, arq in os.walk(original):
    for arch in arq:
        path = dir + arch
        new = new + arch
        
        shutil.move(src=path, dst=new)

i receive this error:

OSError: [Errno 18] Invalid cross-device link '/home/guardian/shared/TestPath/New/img1.jpg' ->  '/media/guardian/GUARDIANT1/New/img1'
  • 1
    Use `shutil.copy()` followed by `os.remove()`. It's a bug in `shutil.move()`, see the comments [here](https://stackoverflow.com/questions/42392600/oserror-errno-18-invalid-cross-device-link) – Barmar Feb 07 '23 at 19:56
  • 3
    Thus, duplicate of [OSError: \[Errno 18\] Invalid cross-device link](https://stackoverflow.com/questions/42392600/oserror-errno-18-invalid-cross-device-link) – chickity china chinese chicken Feb 07 '23 at 20:02
  • @chickitychinachinesechicken it's not a copy of this question, please read my post again, that solution works only for normal directories, but for USB transfer it doesn't work – Jorge Augusto Wilchen Feb 07 '23 at 20:06
  • @JorgeAugustoWilchen - read the comments to the accepted answer. It **is** the same problem. Do as Barmar suggested. – tink Feb 07 '23 at 20:26
  • I tried using that solution, but as I said before, it only worked in computer repositories, in pendrive repositories this error appears – Jorge Augusto Wilchen Feb 07 '23 at 20:39
  • In that case another alternative cause of the problem might be that what you're *trying* to copy aren't files but symlinks? – tink Feb 07 '23 at 20:41
  • I made other changes, and the error is in the name of the files, it are saved in this format "yyyy/MM/DD - HH:MM:SEC.ms", this type of name that causes the file transfer error – Jorge Augusto Wilchen Feb 13 '23 at 13:17
  • after editing the name of the files, this method is effective – Jorge Augusto Wilchen Feb 13 '23 at 13:18

0 Answers0