Python access and list the files in network share with username and password
<p>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.</p>

<pre><code>    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) &gt; 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 *********************"
</code></pre>

<p>Still I have a Problem with NET USE connection command listed down</p>

<pre><code>    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)
</code></pre>

<p>Finally I solved my problem using pywin32 module. Below is the code for that.</p>

<pre><code>    def wnet_connect(host, username, password):
        unc = ''.join(['\\\\', host])
        try:
            win32wnet.WNetAddConnection2(0, None, unc, None, username, password)
        except Exception, err:
            if isinstance(err, win32wnet.error):
                # Disconnect previous connections if detected, and reconnect.
                if err[0] == 1219:
                    win32wnet.WNetCancelConnection2(unc, 0, 0)
                    return wnet_connect(host, username, password)
            raise err

    # Ensure required data files are present in Isilon landing zone of that application context(Eg: gwhp)
    def isLZFilesExist(SHAREPATH):
        wnet_connect(HOST, USER, PASS)        
        path = ''.join(['\\\\', HOST, '\\', SHAREPATH.replace(':', '$')])
        fileList = []
        if os.path.exists(path):
            for (dirpath, dirnames, filenames) in walk(path):
                fileList.extend(filenames)
                break
            if (len(fileList) &gt; 1):
                print fileList
                print "\n\n *********************Required data files are present in the FileShare of that application context(Eg: gwhp)*********************"
                excelResult[0]='PASS'
            else:
                print "\n\n ********************* No files are present to start the Next Run *********************"
                sys.exit(0)
</code></pre>

python python-2.7 subprocess