'os.path.isdir() returns false

I'm trying to check if a folder exists by

path = r"This PC\Bassel's Note\Internal storage\Audiobooks"
print(os.path.isdir(path))

It always returns false even though the folder exists (It's the folder of my phone which is connected to the laptop). How to solve this?



Solution 1:[1]

Paths don't start with This PC. Use r'C:\Users\whatever' instead. Or, to access your phone, use an appropriate protocol/library, e.g. MTP.

Solution 2:[2]

You cannot use the os.path command, since your mobile phone is not a part of your computer's OS. You should use adb to try to list the folder's content and parse the result: if the folder exsits you'll get return code 0, meaning the command was executed successfully, otherwise you'll get a none 0 code.
Pay attention to the folder's name - it must be the name of the folder as it is in the phone's os, and not as it reflects in your computer:

def check_phone_folder(folder):
    return os.system(f'adb shell "ls {folder} >/dev/null 2>&1"') == 0

Now if you call the function with an existing folder name you'll get True - check_phone_folder('/data/local/tmp').
The >/dev/null 2>&1 part will supress the output of the ls command.
You also have to have adb installed in your computer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 TDG