Python os.link() method
Python OS file/directory methods
Overview
The os.link() method creates a hard link named by the parameter dst pointing to the parameter src.
This method is very useful for creating a copy of an existing file.
Only supported on Unix and Windows.
Syntax
The **link()** method syntax is as follows:
os.link(src, dst)
Parameters
-
src -- The source address used to create the hard link
-
dst -- The target address used to create the hard link
Return Value
This method has no return value.
Example
The following example demonstrates the use of the link() method:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# Open file
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )
# Close file
os.close( fd )
# Create copies of the above files
dst = "/tmp/foo.txt"
os.link( path, dst)
print "创建硬链接成功!!"
The output of the above program is:
创建硬链接成功!!