2016-08-26 18 views
0

Python言語を使用して、指定したFileShareの場所からファイル名の一覧を取得します(唯一)。ここに私のコードスニペットがありますが、それはすべてのファイルのリストを実行していません。Pythonがアクセスし、ユーザー名とパスワードでネットワーク共有のファイルを一覧表示します。

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 *********************" 

はまだ私は最後に、私はpywin32モジュールを使用して私の問題を解決したダウン

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) 
+0

トライR '\\ファイル共有の\の場所'、または他の、エラーメッセージは何ですか? – Fred

+0

はい、動作します。迅速なお返事をありがとう。 しかし、コードを切断する方法、つまり「バックアップストレージが既に接続されている」というメッセージが表示されたので、「NET USE」オプションでチェックできませんでした。 接続解除スクリプトで提案してください! –

+0

小さな注意:SQLSR_USER/PATHとBACKUP_REPO_PATHは、Webサービスなどのようにリモートで埋め込むことはできません(または完全にチェックすること)。そのos.system呼び出しは、いくつかの厄介なトラブルにつながる可能性があります。 –

答えて

0

記載されているNET USE接続コマンドに問題があります。以下はそのコードです。

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) > 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) 
関連する問題