Bootstrap FreeKB - Python (Scripting) - Create a symbolic link using os.symlink
Python (Scripting) - Create a symbolic link using os.symlink

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 1f3417 in the box below so that we can be sure you are a human.