Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 os.link() Method

Python3 os.link() Method

Python3 OS File/Directory Methods Python3 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

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

# 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:


创建硬链接成功!!

Python3 OS File/Directory Methods Python3 OS File/Directory Methods