OSError: [Errno 18] Invalid cross-device link
Asked Answered
T

2

112

I'm working with django 1.6.5 and python 2.7. I have import feature in my app and I get error:

OSError: [Errno 18] Invalid cross-device link

I have problem with this part of code:

os.rename(db_temp, settings.DATABASES['bookmat']['NAME'])

code in settings:

'bookmat': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': '/my_projects/book/db/bookmat.sqlite3',
},
Townshend answered 22/2, 2017 at 13:17 Comment(4)
What is db_temp valueTeratogenic
db_temp = settings.DATABASES[db_temp_name]['NAME'] @TeratogenicTownshend
initial db_temp value as stringTeratogenic
I stumbled over this when I tried to move something from folder within a Docker container to a volume.Belike
S
161

os.rename only works if source and destination are on the same file system. You should use shutil.move instead.

Sacken answered 14/5, 2017 at 18:51 Comment(7)
My version if shutil.move() is actually implemented in terms of os.rename(). Is there another alternative to use?Genuine
I'm using shutil.move and still getting this errorSchopenhauer
As Kilian says shutil.move() with different file systems gives the same error as os.rename(). Easiest to use shutil.copy() and os.remove() instead.Retorsion
shutil.move in Python 3 handles the foreign filesystem correctly by using copy and delete, per documentation.Pelletier
@Pelletier it doesn't handle it properly in my case, at least on python3 with a regular ext4 and a folder mounted with rclone mount.Guadalupeguadeloupe
@Ave @Pelletier I have also seen this: changed from os.rename() to shutil.move() and get the same OSError. And the backtrace shows that it calls os.rename() @kilian-fothSymbology
Or, the actual exception is a different one, and you get shown the trace as os.rename, as the implementation tries os.rename, and then handles the copy+unlink in the except section. So if another exception occurs within the except-block, you get both exceptions in the stack, e.g. when OSError: [Errno 116] Stale file handle is thrown by the attempt to copy.Linell
R
15

rename only works when the source and target names are on the same file system. You probably have different mounts. Otherwise you get that error. You can implement the same effect with a copy and a delete.

Reviewer answered 22/2, 2017 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.