Python (Scripting) - Create a symbolic link using os.symlink
by
Jeremy Canfield |
Updated: April 01 2024
| Python (Scripting) articles
os.symlink can be used to create a symbolic link. Here is the minimal boilterplate code without error handling.
#!/usr/bin/python3
import os
src="/usr/local/bin/kubectl1.23.6"
dst="/usr/local/bin/kubectl"
os.symlink(src, dst)
Here is a more practical example, with try/except/else error handling.
#!/usr/bin/python3
import os
src="/usr/local/bin/kubectl1.23.6"
dst="/usr/local/bin/kubectl"
try:
os.symlink(src, dst)
except Exception as exception:
print(f"Got the following exception when attempting to create a symbolic link (symlink) from {src} to {dst} - {exception}")
else:
print(f"Successfully created a symbolic link (symlink) from {src} to {dst}")
Did you find this article helpful?
If so, consider buying me a coffee over at