Explanation of pysftp.Connection.walktree() parameters
Asked Answered
G

2

9

I have just started using Python's pysftp and I am confused as how to call it's walktree function.

I found some code (found at http://pydoc.net/Python/pysftp/0.2.8/pysftp/) that helped me better understand what form my parameters should take

def walktree(self, remotepath, fcallback, dcallback, ucallback, recurse=True):
    '''recursively descend, depth first, the directory tree rooted at
    remotepath, calling discreet callback functions for each regular file,
    directory and unknown file type.

    :param str remotepath:
        root of remote directory to descend, use '.' to start at
        :attr:`.pwd`
    :param callable fcallback:
        callback function to invoke for a regular file.
        (form: ``func(str)``)
    :param callable dcallback:
        callback function to invoke for a directory. (form: ``func(str)``)
    :param callable ucallback:
        callback function to invoke for an unknown file type.
        (form: ``func(str)``)
    :param bool recurse: *Default: True* - should it recurse

    :returns: None

But I am still confused on what exactly is meant by "callback function to invoke for a regular file, for a directory, and for an unknown file type.

I have also looked through the official documentation: https://media.readthedocs.org/pdf/pysftp/latest/pysftp.pdf

but all it tells me about the walktree() function is that:

Is a powerful method that can recursively (default) walk a remote directory structure and calls a user-supplied callback functions for each file, directory or unknown entity it encounters. It is used in the get_x methods of pysftp and can be used with great effect to do your own bidding. Each callback is supplied the pathname of the entity. (form: func(str))

which I felt did not give me much information on how to call it properly.

If someone could provide an example of calling this function correctly and an explanation of why you are passing your chosen arguments, it would be greatly appreciated!

Glycerite answered 28/10, 2014 at 3:56 Comment(0)
S
7

Here is the sample code you are looking for.

import pysftp

file_names = []
dir_names = []
un_name = []

def store_files_name(fname):
    file_names.append(fname) 

def store_dir_name(dirname):
    dir_names.append(dirname)

def store_other_file_types(name):
    un_name.append(name)

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
sftp = pysftp.Connection(host="Your_ftp_server_name", username="ftp_username", private_key="location_of_privatekey", cnopts=cnopts)
sftp.walktree("/location_name/",store_files_name,store_dir_name,store_other_file_types,recurse=True)
print file_names,dir_names,un_name

file names, directory names and unknown file types are stored in lists file_names, dir_names and un_name respectively.

Selfsame answered 21/3, 2019 at 15:28 Comment(0)
Z
0

Learn what is the callback, if that's the actual problem.

To all three arguments of walktree, you need to pass a reference to a function that takes a single string argument. As walktree recurses the directory structure, it "calls back" one of those functions for every file-system object it finds, passing path to the object as the (string) argument.

Typically, you will need some state (context) for the function implementation. I.e. a reference to a container to store the paths found. To avoid using global variables, the pysftp sample, you referred to in your question, passes in methods of helper class instead of plain functions, keeping the state (the flist, dlist and ulist containers) in the object instance.

Zephan answered 29/10, 2014 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.