Using Python I want to create a symbolic link pointing to a path that does not exist. However os.symlink just complains about "OSError: [Errno 2] No such file or directory:".. This can easily be done with the ln program, but how to do it in Python without calling the ln program from Python?
Edit: somehow I really messed this up :/ ... both answers below is correct
Such error is raised when you try to create a symlink in non-existent directory. For example, the following code will fail if /tmp/subdir
doesn't exist:
os.symlink('/usr/bin/python', '/tmp/subdir/python')
But this should run successfully:
src = '/usr/bin/python'
dst = '/tmp/subdir/python'
if not os.path.isdir(os.path.dirname(dst)):
os.makedirs(os.path.dirname(dst))
os.symlink(src, dst)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With