Skip to content Skip to sidebar Skip to footer

How To Set Pattern Matching In Python's Smbcoonect Listpath Function

SMBConnect has the following function, listPath, which lists out the the contents of a given directory. listPath(service_name, path, search=55, pattern='*', timeout=30) Retriev

Solution 1:

So, we got it to work with the re class as a work-around from the SMBConnection listPath function by using a variation of this that works with the created SMBConnection object. the ListPath function is still used but just not the pattern part of it. I built an "If-else" structure to handle the arg input and regex.

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 

Solution 2:

The original poster wasn't specifying the pattern parameter in the listpath() method correctly.

Instead of this:

files = newConn.listPath('C$', '/' + 'testing', '*.pdf')

It should be like this:

files = newConn.listPath('C$', '/' + 'testing', pattern='*.pdf')

Actually I asked a question in another thread if it is possible to filter on more than one file extension possibly by way of a regular expression. The author of pysmb responded to say this is not possible.

To filter on more than one file extension the workaround created by the original poster to this thread could be used.

Post a Comment for "How To Set Pattern Matching In Python's Smbcoonect Listpath Function"