我想使用Python语言从给定的FileShare位置获取文件名列表(仅限)。这是我的代码段,但它没有运行列出任何文件。
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 *********************"
我仍然遇到列出的NET USE连接命令问题
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 :(得分:0)
最后我用pywin32模块解决了我的问题。以下是该代码。
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)