How to get the files inside a directory link with python

Question:

This is probably super easy, but after an hour of trying, I haven’t been able to find it.

From a specific folder inside a database called /Database/Images/, I manually created a folder link with Windows and placed it at, /Tests/Images.

With that done, I created a python script inside the /Tests folder and I’m trying to use it to access the files inside that /Tests/Images link.

How can I get the data inside /Database/Images/ from the link /Tests/Images?

The problem is that I can’t use os.listdir(), because it is not a directory, but I also can’t seem to find the way to know that /Tests/Images is actually pointing to /Database/Images/

So far I’ve tried several os function but

  • os.listdir() returns that /Tests/Images is not a directory
  • os.path.islink() returns False
  • os.path.realpath() and os.path.abspath() returns /Tests/Images, not /Database/Images/
Asked By: Guillermo López

||

Answers:

It seems you’re maybe dealing with a Windows shortcut (.lnk) file, not a symbolic link (I will mention how to make that later in the answer). The os module functions you’ve mentioned are designed to work with symbolic links, not Windows shortcuts.

You CAN work with Windows shortcuts by using the pylnk3 package. You can install it using pip: pip install pylnk3. here is an example of how you use it:

import os
import pylnk3

def get_shortcut_target(shortcut_path):
    with open(shortcut_path, "rb") as f:
        lnk = pylnk3.parse(f)
        return lnk.relative_path

shortcut_path = r"C:TestsImages.lnk"  # This should be the path to your shortcut
target_path = get_shortcut_target(shortcut_path)

print(f"Target path: {target_path}")
print("Files in target directory:")
print(os.listdir(target_path))

This script defines a function get_shortcut_target() that uses pylnk3 to parse the target path from a Windows shortcut file. Then, you can use os.listdir() with the target path to list the files in the target directory.

If you want to create a symbolic link instead of a Windows shortcut, you can use the mklink command in the Command Prompt (with administrator privileges) or the os.symlink() function in Python.

The syntax to create symbolic links is mklink /D link_path target_path. The /D flag is used to create a directory symbolic link. For example:

mklink /D C:TestsImages C:DatabaseImages

This command will create a symbolic folder link named Images in the C:Tests directory, pointing to the C:DatabaseImages directory.

And here is how you create a symbolic link using python (remember you need to run it with admin privileges or else you will get a PermissionError)

import os

link_path = r"C:TestsImages"
target_path = r"C:DatabaseImages"

os.symlink(target_path, link_path, target_is_directory=True)
Answered By: Johan Abdullah Holm
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.