【发布时间】:2016-08-26 09:24:17
【问题描述】:
我想使用 Python 语言从给定的 FileShare 位置获取文件名列表(仅限)。这是我的代码 sn-p,但它没有运行列出任何文件。
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)
【问题讨论】:
-
try r'\\fileshare\location',否则,错误信息是什么?
-
是的,它有效。感谢你的及时回复。但是,如何断开它,这意味着在我的代码中我已经提示“备份存储已连接”,所以我无法检查“NET USE”选项请建议使用断开脚本!
-
小提示:要格外小心,SQLSR_USER/PATH 和 BACKUP_REPO_PATH 不能像 web 服务或其他东西一样远程填写(或彻底检查它们)。那个 os.system 调用可能会给你带来一些麻烦
-
"backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)" 该代码块返回 false,因为该远程路径未经过身份验证。
标签: python python-2.7 subprocess