os.link makes a copy, not a link

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


Carl Banks <invalidemail at aerojockey.com> wrote:
>  Dan M 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
> 
>  Are file1 and file2 on the same filesystem?  Looks like os.link just
>  calls the OS's link system call, which, for your system, might copy the
>  file.

The link(2) system call on linux never does that.  Eg

>>> import os
>>> file("z", "w").write("test")
>>> os.link("z", "/dev/shm/z")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OSError: [Errno 18] Invalid cross-device link
>>

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



More information about the Python-list mailing list