[Python] Python access and list the files in network share with username and password

Discussão em 'Python' iniciado por Stack, Agosto 15, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to get list of filenames(Only) from the given FileShare location using Python Language. Here is my Code snippet, but it is not running listing any files.

    import os
    from os import walk
    SQLSR_USER= 'username'
    SQLSR_PASS= 'password'
    BACKUP_REPOSITORY_PATH= '\\fileshare\location'
    fileList = []
    backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)
    if backup_storage_available:
    print("Backup storage already connected.")
    else:
    print("Connecting to backup storage.")

    mount_command = "net use /user:" + SQLSR_USER + " " + BACKUP_REPOSITORY_PATH + " " + SQLSR_PASS
    os.system(mount_command)
    backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)
    if backup_storage_available:
    print ("Connection success.")
    else:
    raise Exception("Failed to find storage directory.")

    for (dirpath, dirnames, filenames) in walk(backup_storage_available):
    fileList.extend(filenames)
    break

    if (len(fileList) > 1):
    print "\n\n *********************Required data files are present in the FileShare*********************"

    else:
    print "\n\n ********************* No files are present to start the Next Run *********************"


    Still I have a Problem with NET USE connection command listed down

    mount_command = "net use /user:" + SQLSR_USER + " " + BACKUP_REPOSITORY_PATH + " " + SQLSR_PASS
    os.system(mount_command)
    backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)

    Continue reading...

Compartilhe esta Página