python - 如何在Python的SMBCoonect listPath函数中设置模式匹配

标签 python regex python-3.x

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 path

path (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/

相关文章:

c# - 删除字符串中的特殊字符和无效字符

python-3.x - 如何在 Mac 上更新 Tensorflow?

Python 3 : win32com object. 大小抛出异常 (pywintypes.com_error)

python - 如何解析主机 :port pair in Python

python - 如何将以 24 作为小时值的日期/时间字符串转换为 Pandas 中的日期时间?

python - 显示网页中的所有链接

linux - 免root升级glibc

python - 如何从运行时代码访问 PyInstaller Hook 文件命名空间中的变量?

.net - 未找到字段 : System. Text.RegularExpressions.Regex.internalMatchTimeout

java - ${(stringToMatch)} 的正则表达式