Python| os.link() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.link()方法用于创建硬链接。此方法创建一个指向名为destination的源的硬链接。
要了解硬链接,请参阅本文。
注意:此方法仅适用于 Windows 和 Unix 平台。
Syntax: os.link(src, dst, *, src_dir_fd = None, dst_dir_fd = None, follow_symlinks = True)
Parameters:
src: A path-like object representing the file system path. This is the source file path for which the hard link will be created
dst: A path-like object representing the file system path. This is the target file path where hard link will be created.
A path-like object is a string or bytes object which represents a path.
src_dir_fd (optional): A file descriptor referring to a directory.The default value of this parameter is None. If the specified src path is absolute then this parameter is ignored. If the specified src path is relative and src_dir_fd is not None then the specified src path is relative to the directory associated with src_dir_fd.
dst_dir_fd (optional): A file descriptor referring to a directory.
follow_symlinks (optional) : A Boolean value.
Return type: This method does not return any value.
代码: os.link()方法的使用
Python3
# Python program to explain os.link() method
# importing os module
import os
# Source file path
src = '/home/ihritik/file.txt'
# Destination file path
dst = '/home/ihritik/Desktop/file(link).txt'
# Create a hard link
# pointing to src named dst
# using os.link() method
os.link(src, dst)
print("Hard link created successfully")
Hard link created successfully
参考: https://docs。 Python.org/3/library/os.html#os.link