Python os.symlink() Method
-
Syntax of the
os.symlink()
Method -
Example 1: Use the
os.symlink()
Method in Python -
Example 2: Shorter Syntax for the
os.symlink()
Method in Python
Python os.symlink()
method is an efficient way of making a symbolic link. When working with file systems, it is important to have symbolic links to add links to specified paths or folders.
Syntax of the os.symlink()
Method
os.symlink(source, destination)
Parameters
source |
It is an address object of a file system path or a symlink to which you want to point. |
destination |
It is an address object of a file system path or a symlink that will have the symbolic link. |
Return
In the execution process, this method does not return any value.
Example 1: Use the os.symlink()
Method in Python
import os
source = "C:/Users/786/Documents/File.txt"
destination = "C:/Users/786/Desktop/NewFile.txt"
os.symlink(source, destination)
print("The symbolic link has been created successfully")
Output:
An OSError
might occur while using the os.symlink()
method due to invalid or inaccessible processes and paths. To avoid this error, you must run the compiler or Python interpreter as administrator.
Example 2: Shorter Syntax for the os.symlink()
Method in Python
import os
os.symlink("C:/Users/786/Documents/File.txt", "C:/Users/786/Desktop/NewFile.txt")
print("The symbolic link has been created successfully")
Output:
The os.symlink()
method has two optional parameters that are rarely used. They specify if the destination path is a directory, and a file descriptor refers to a valid directory.
Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.
LinkedIn