user1016942
user1016942

Reputation:

Rename symbolic link in python

I would like to know is it possible to rename a symlink with python.

Already tried os.rename and shutil.move

Any ideas?

os.rename return me this error : OSError: [Errno 18] Cross-device link

>>> import sys, os
>>> 
>>> path = '/Library/Application Support/appsolute/MAMP PRO/db/'
>>> job = path + 'mysql-job/'
>>> perso = path + 'mysql-perso/'
>>> mysql = path + 'mysql/'
>>> 
>>> os.rename(mysql, job)

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    OSError: [Errno 18] Cross-device link
>>> exit()

Danny-Dombrowski:script ddombrowski$ ls -al /Library/Application\ Support/appsolute/MAMP\ PRO/db/
    total 24
    drwxrwxr-x   5 root         admin   170  7 fév 19:29 .
    drwxrwxr-x  12 root         admin   408  7 fév 17:14 ..
    -rw-r--r--@  1 ddombrowski  admin  6148  7 fév 19:29 .DS_Store
    lrwxr-xr-x   1 ddombrowski  admin    46  7 fév 19:29 mysql -> /Volumes/Gestion Portail         Sante/Database/mysql/
    drwxrwxr-x  11 ddombrowski  admin   374  7 fév 19:22 mysql-perso

Upvotes: 2

Views: 3185

Answers (2)

phihag
phihag

Reputation: 287865

os.rename will work fine:

$ ln -s target link
$ python -c "import os; os.rename('link', 'link.new')"
$ ls -l link.new
lrwxrwxrwx 1 phihag phihag 6 Feb  8 01:25 link.new -> target

Make sure you don't include a / after the symlink: link/ is the same as link/., and not the same as link.

Upvotes: 2

Xupeng
Xupeng

Reputation: 371

os.rename should work.

xupeng@xupeng t $ ls -l
total 0
-rw-r--r-- 1 xupeng xupeng 0 Feb  8 08:22 a
lrwxrwxrwx 1 xupeng xupeng 1 Feb  8 08:23 b -> a
xupeng@xupeng t $ python
Python 2.6.5 (release26-maint, Sep 21 2011, 10:32:38) 
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.rename('b', 'c')
xupeng@xupeng t $ ls -l
total 0
-rw-r--r-- 1 xupeng xupeng 0 Feb  8 08:22 a
lrwxrwxrwx 1 xupeng xupeng 1 Feb  8 08:23 c -> a

Upvotes: 3

Related Questions