Python os.symlink() Method

Python os.symlink() Method

  1. Syntax of the os.symlink() Method
  2. Example 1: Use the os.symlink() Method in Python
  3. 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.

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.

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:

os symlink

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.

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:

shorter syntax os symlink

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 Waseem avatar Musfirah Waseem avatar

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

Related Article - Python OS

  • Python os.set_handle_inheritable() Method
  • Python os.set_inheritable() Method
  • Python os.stat_result Class
  • Python os.renames() Method
  • Python os.get_handle_inheritable Method
  • Python os.get_inheritable Method