
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Soft Link of a File Using Python
Creating symbolic links (or soft links) in Python can be done using the OS module. A symbolic link is a special type of file that points to another file or directory, allowing you to access it under a different pathname. This can be useful for various reasons, such as creating shortcuts or managing different versions of files.
The following methods provide different ways to create symbolic links.
- Using os.symlink() Function
- Using pathlib.Path.symlink_to() Function
- Using Command Line with subprocess Module
Using 'os.symlink()' Function
The most straightforward way to create a symbolic link is by using the os.symlink(src, dst) function. we have to provide the path of the original file as the first argument. The second argument is the name you want to give to the symbolic link.
- src: This is the path to the original file you want the link to point.
- dst: This is the path for the new symbolic link you are creating.
Example
After running the following code, you will find 'my_photo.jpg' in the same directory, which acts as a shortcut to 'photo.jpg'.
import os # Source file src = 'photo.jpg' # Destination link dst = 'my_photo.jpg' # Create a symbolic link os.symlink(src, dst)
Using 'pathlib.Path.symlink_to()' Function
The pathlib module provides an object-oriented approach to file system paths and includes a method for creating symbolic links.
We can create a symbolic link using the symlink_to() method of a Path object. Set the source file and the desired link name as Path objects.
Example
In the following example, the method uses 'Path' objects, which makes it easier to work with file paths. The symlink_to(src) method creates a symbolic link at the destination path ('my_photo_link.jpg') that points to the source file ('photo.jpg').
The outcome will be similar; running this code will also create 'my_photo_link.jpg' that points to 'photo.jpg'.
from pathlib import Path # Source file src = Path('photo.jpg') # Destination link dst = Path('my_photo_link.jpg') # Create a symbolic link dst.symlink_to(src)
Using Command Line with 'subprocess' Module
We can also use Python's subprocess module to call the system's command-line tools for creating symbolic links, which can be useful if you're working in an environment where the 'os.symlink()' function has limitations.
This method allows us to run the ln -s command, which is common in Unix-like systems. You specify the original file and the new link name in a list passed to subprocess.run().
Example
In the following example, the subprocess.run() function executes the command as if it were typed in the command line. The 'check=True' flag raises an error if the command fails.
This code will create another symbolic link called 'my_photo_exe_link.jpg' pointing to 'photo.jpg', similar to the previous methods.
import subprocess # Source file src = 'photo.jpg' # Destination link dst = 'my_photo_exe_link.jpg' # Create a symbolic link using the system command subprocess.run(['ln', '-s', src, dst], check=True)