SMBConnect 有以下函数 listPath,它列出给定目录的内容。
listPath(service_name, path, search=55, pattern='*', timeout=30) Retrieve a directory listing of files/folders at path
Parameters:
service_name (string/unicode) – the name of the shared folder for the pathpath (string/unicode) – path relative to the service_name where we are interested to learn about its files/sub-folders.
search (integer) – integer value made up from a bitwise-OR of SMB_FILE_ATTRIBUTE_xxx bits (see smb_constants.py). The default search value will query for all read-only, hidden, system, archive files and directories.
pattern (string/unicode) – the filter to apply to the results before returning to the client.
Returns:
A list of smb.base.SharedFile instances.
newConn=SMBConnection(arguments.username, password, DEFAULT_CLIENT_NAME, arguments.hostname, domain=arguments.domain,
use_ntlm_v2=True, is_direct_tcp=True)
assert newConn.connect(ip_address, 445, timeout=60)
files = newConn.listPath('C$', '/' + 'testing', '*.pdf')
for file in files:
print(file.filename)
我无法将模式匹配更改为任何特定内容。上面,我只想打印列表中包含“.pdf”的文件名。相反,当代码执行时,我只是获取所有文件。没有错误或任何东西。我尝试过带或不带“*”和“.”并得到相同的结果。
最佳答案
因此,我们通过使用与创建的 SMBConnection 对象一起使用的变体,将其与 re 类一起使用,作为 SMBConnection listPath 函数的解决方法。 ListPath 函数仍然被使用,但只是不再使用它的模式部分。我构建了一个“If-else”结构来处理 arg 输入和正则表达式。
extensions = ['pdf', 'doc'] filenames = ['foobar.pdf', 'bar.doc'] for extension in extensions: compiled = re.compile('\.{0}$'.format(extension)) for filename in filenames: results = re.search(compiled, filename) print results
关于python - 如何在Python的SMBCoonect listPath函数中设置模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51141485/