Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of pysftp.Connection.walktree() parameters

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!

like image 467
busebd12 Avatar asked Oct 28 '14 03:10

busebd12


People also ask

What is Pysftp?

SFTP, abbreviated for SSH File Transfer Protocol and known as Secure File Transfer Protocol, is a network protocol that allows us to access files, transfer them and manage them over any dependable data stream.

How do I use Pysftp?

You can list the content of the directory using pysftp in Python. For that, you need your hostname, username, and password. Then you need to switch from the directory using either the cwd or chdir method and provide the remote directory as the first argument.

Can Pysftp connect to FTP?

You are connecting to FTP server in FileZilla. pysftp is SFTP library. FTP and SFTP are completely different protocols. To connect with FTP in Python, use ftplib.


1 Answers

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.

like image 117
Nijil Avatar answered Sep 21 '22 16:09

Nijil