Python3 os.link() Method

Python3 OS File/Directory Methods


Overview

The os.link() method creates a hard link, named by the parameter dst, that points to the parameter src.

This method is very useful for creating a copy of an existing file.

It is only supported on Unix and Windows.

Syntax

link()The syntax format of the method is as follows:

os.link(src, dst)

Parameters

  • src-- The source path used to create the hard link

  • dst-- The destination path 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/python3

import os, sys

# 打开文件
path = "/var/www/html/foo.txt"
fd = os.open( path, os.O_RDWR|os.O_CREAT )

# 关闭文件
os.close( fd )

# 创建以上文件的拷贝
dst = "/tmp/foo.txt"
os.link( path, dst)

print ("创建硬链接成功!!")

The output of executing the above program is:

Hard link created successfully!!

Python3 OS File/Directory Methods

Other Extensions