pythonpathexistssmb

With Python, how to use smbclient to check remote folder/file existence


What I try to do is I want to check my_new_folder exists or not with smbclient first before I create it.

import smbclient
username = 'username'        # Domain user name 
password = "password"        # Domain user password

smbclient.register_session(server="remote_server_name", username=username, password=password, port=445,auth_protocol='ntlm')
new_shared_subfolder_path="//remote_server_name/existing_shared_folder/my_new_folder"
## How to check new_shared_subfolder_path exists or not with smbclient, before I create it?  
smbclient.mkdir(new_shared_subfolder_path)  

Thank you very much!


Solution

    1. try to use makedirs

    if my_new_folder does not exist, then create it

    smbclient.makedirs(my_new_folder , exist_ok=True)

    2.

    check file existence

    print(smbclient.path.exists(file_path))

    check whether the path is a folder

    print(smbclient.path.isdir(file_path))