Python Forum

Full Version: "os.symlink(targetdir,destfile)" VS "ln -s targetdir destfile" (not equivalent?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
Unfortunately the issue is not reproducible. I don't think os.symlink() gives a different result from ln -s. Can you check this again?