Python 3.7 doc for os.symlink(src, dst)
is Create a symbolic link pointing to src named dst.
Let's imagine this working directory :
+-- nb.ipynb
+-- dir1
| +-- file1
+-- dir2
Let's suppose I want to create a symbolic link in dir2
called filed2
pointing to dir1/file1
from my notebook nb.ipynb
.
In a cell of the notebook, i'll put:
import os
os.symlink('dir1/file1', 'dir2/file2')
However, this won't work, this will create a file2 in dir2, but when you look at the properties of this file, its Type
is Link(broken) (inode/symlink)
and its Link target
is dir1/file1
. The Link target is what you put as src in os.symlink.
To achieve the right symlink, one would need to do
os.symlink('../dir1/file1', 'dir2/file2')
which means that os.symlink(src, dest)
, src
should contain the path of the source relative to the destination.
Since I had to create such symlink multiple times, I created this function :
def symlink_rel(src, dst):
rel_path_src = os.path.relpath(src, os.path.dirname(dst))
os.symlink(rel_path_src, dst)
Calling
symlink_rel('dir1/file1', 'dir2/file2')
will create the right symbolic link. It does seem to me that os.symlink doesn't behave like the documentation says so. Why does os.symlink
need src
to be path of src
relative to dst
rather than src
and dst
to be path from the working directory?
Symbolic links can either be absolute or relative links. Absolute links are links that specify each portion of the path name; relative links are determined relative to where relative–link specifiers are in a specified path.
Relative Symbolic Link : An relative symbolic link is an symbolic link, whose target has relative path. An relative path is path that is based on Current Working Directory. The path of target file/folder will be calculated according to the current path of symlink. Therefore, there is no definite path so as to say.
A symbolic link a reference to another file or directory in a form of absolute or relative path. A symbolic link is a link that points to a specified entry (source). Syntax: ln -s target_path link_path. Example: Unlike hard links, there is a visual difference as seen above.
The reason you get "xyz-file: can make relative symbolic links only in current directory" is because for the source directory, you specified a relative path. It'll work as you want it if you specify an absolute path for the source, like so: "cp -sR /root/absolute/path/name dest". Does not work on all systems.
os.symlink('dir1/file1', 'dir2/file2')
Soft links saves the original location of the file which means "dir2/file2" saves the location "dir1/file1". When you cat the file "dir2/file2" this will search for the "dir1/file1" inside the directory "dir2/" which is the working directory of "file2" and there is no path inside the dir2 called "dir1/file1"
os.symlink('../dir1/file1', 'dir2/file2')
when you cat this file dir2/file2 , the parent directory of the "file2" is "dir2". which is same as above. The difference is that ".." means go one directory from where you are standing right now. From that location goto "dir1/file1" so it will work.
Don't consider your working director, consider the softlinks parent directory and read the location based on that
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With