How do I link directories in python (Linux cmd ln -s equivalent)? [duplicate]
Asked Answered
E

1

5

I'm translating Linux commands to a python script. How would I perform the Linux command 'ln -s'

CMD:

    ln -s /users/me/link_file hello 

I want to link 'hello' to 'link_file' in python

Thanks!

Erleena answered 8/3, 2018 at 21:27 Comment(4)
If you actually want to "perform the Linux command", see the subprocess module. If you want to create a symlink in the same way the Linux command does, see the os or pathlib module.Montcalm
why don't you try it?Bernardabernardi
Just want to make sure... Can't try it without knowing for sure because the environment is delicate.Erleena
I think it is good to find the simple answer here on stackoverflow. The two questions are not the same...Preciousprecipice
B
15

os.symlink() is the provided function for creating symlinks. They are created as such:

import os
os.symlink(src,dst)

Where src and dst are the paths.

See the documentation here for more info.

Bowse answered 8/3, 2018 at 21:32 Comment(2)
"Create a symbolic link pointing to src named dst."Preciousprecipice
The answer provided doesn't work if provided src path is relative - it creates broken link. Use following instead: import os os.symlink(os.path.abspath(src), dst)Virgel

© 2022 - 2024 — McMap. All rights reserved.