asfa afs
asfa afs

Reputation: 21

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?

Upvotes: -1

Views: 517

Answers (2)

Sören
Sören

Reputation: 2433

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.

Upvotes: 1

TDG
TDG

Reputation: 6151

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.

Upvotes: 0

Related Questions