Parameter explanation pysftp.Connection.walktree ()

I just started using pysftp Python, and I am confused how to call it walktree function.

I found 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'm still confused about what exactly is meant by the “callback function” for a regular file, for a directory, and for an unknown file type.

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

but all he tells me about the walktree() function is this:

It is a powerful method that can recursively (by default) navigate through a remote directory structure and call user-provided callback functions for each file, directory, or unknown object it encounters. It is used in the get_x pysftp methods and your own bets can be used with great effect. Each callback is provided with an organization path. (form: func(str) )

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

If someone can provide an example of the correct work with this function and an explanation of why you pass the arguments you have chosen, we will be very grateful!

+6
source share
2 answers

Find out what a callback is if this is an actual problem.

For all three walktree arguments walktree you need to pass a reference to a function that takes one string argument. Because walktree refers to a directory structure, it "calls" one of these functions for each walktree of the file system object, passing the path to the object as an (string) argument.

Typically, you need some state (context) to implement the function. Those. link to the container for storing found paths. To avoid the use of global variables, the pysftp example that you referred to in your question, instead of simple functions, passes methods of the auxiliary class, storing the state ( flist , dlist and ulist ) in the instance of the object.

0
source

Here is an example of the 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 the file_names , dir_names and un_name lists, respectively.

0
source

Source: https://habr.com/ru/post/1205656/


All Articles