Python Forum
"os.symlink(targetdir,destfile)" VS "ln -s targetdir destfile" (not equivalent? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: "os.symlink(targetdir,destfile)" VS "ln -s targetdir destfile" (not equivalent? (/thread-11897.html)



"os.symlink(targetdir,destfile)" VS "ln -s targetdir destfile" (not equivalent? - NobahdiAtoll - Jul-30-2018

Ok so I been working with Linux for a few years now and I have finally decided to put file and folder linking into practice, considering I need files in multiple locations but i don't want to have dupes... So I'm writing a script to create links to files and folders as needed (where needed)

however I've run into a little discrepancy when running my completed script as opposed to manually creating symlinks

I have a folder 'sourcedir' located at the root of a drive and a folder "foo/bar/targetfolder" (relative to root of drive) which needs a link to the sourcedir folder inside it.

first i get the relative path
relativepath = os.path.relpath('/sourcedir','/foo/bar/targetdir')
which sets relatiivepath to '../../sourcedir'

next i create the link

os.symlink(relativepath,'/foo/bar/targetdir/targetlink)')
if I go to my filemanager and delete the created 'targetlink' it starts deleting all the files in the linked folder

however if I create the link in a terminal with
Quote:ln -s relativepath /foo/bar/targetdir/targetlink'
the profuced symlink shows in my filemanager with a shortcut arrow on it and deleting it simply deletes the link and nothing else. This is the behaviour I'm aiming for and I can't figure out how to get python to do that for me.

I'd appreciate any help


RE: "os.symlink(targetdir,destfile)" VS "ln -s targetdir destfile" (not equivalent? - Gribouillis - Jul-31-2018

Unfortunately the issue is not reproducible. I don't think os.symlink() gives a different result from ln -s. Can you check this again?