我刚开始使用 Python 的 pysftp,我对如何调用它的 walktree
函数感到困惑。
我找到了一些代码(位于 http://pydoc.net/Python/pysftp/0.2.8/pysftp/ )帮助我更好地理解我的参数应该采用什么形式
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
但我仍然对“为常规文件、目录和未知文件类型调用的回调函数”的确切含义感到困惑。
我也看了官方文档:https://media.readthedocs.org/pdf/pysftp/latest/pysftp.pdf
但它告诉我关于 walktree()
函数的所有信息是:
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)
)
我觉得这并没有给我太多关于如何正确调用它的信息。
如果有人可以提供正确调用此函数的示例并解释为什么要传递所选参数,我们将不胜感激!
最佳答案
这是您正在寻找的示例代码。
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
、dir_names
和un_name
中。
关于python - pysftp.Connection.walktree() 参数说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26600648/