os.link makes a copy, not a link

Nick Craig-Wood nick at craig-wood.com
Sat Jun 10 04:30:03 EDT 2006


Dan M <dan at catfolks.net> wrote:
>  I'm a little bit confused. According to the sources I've looked at on the
>  net, 
>      os.link('file1', 'file2')
>  should make a hard link from file1 to file2. But what I'm finding is that
>  it's actually making a copy. Am I forgetting a step or something?
> 
>  Python 2.3.4 running on CentOS 4.3

It works here (Py 2.4.3 on Ubuntu Dapper Drake)

The way to check whether you are getting a copy or a hardlink is to
see whether the inode number is the same.

Otherwise it is impossible to tell whether you have a copy or a
hardlink.

>>> import os
>>> file("z", "w").write("test")
>>> os.link("z", "z2")
>>> os.stat("z").st_ino
1685186L
>>> os.stat("z2").st_ino
1685186L
>>> print os.popen("ls -li z z2").read()
1685186 -rw-r--r-- 2 ncw ncw 4 2006-06-10 08:31 z
1685186 -rw-r--r-- 2 ncw ncw 4 2006-06-10 08:31 z2

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list