Is it possible to use os.walk over SSH?

2019-02-23 14:48发布

I'm new to Python so forgive me if this is basic, I've searched but can't find an answer. I'm trying to convert a Perl script into Python (3.x) which connects to a remote server and copies the files in a given directory to the local machine. Integrity of the transfer is paramount and there are several steps built-in to ensure a complete and accurate transfer.

The first step is to get a complete listing of the files to be passed to rsync. The Perl script has the following lines to accomplish this:

@dir_list = `ssh user@host 'find $remote_dir -type f -exec /bin/dirname {} \\;'`;
@file_list = `ssh user@host 'find $remote_dir -type f -exec /bin/basename {} \\;'`;

The two lists are then joined to create $full_list.

Rather than open two separate ssh instances I'd like to open one and use os.walk to get the information using:

for remdirname, remdirnames, remfilesnames in os.walk(remotedir):
    for remfilename in remfilesnames:
        remfulllist.append(os.path.join(remdirname, remfilename))

Thank you for any help you can provide.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-02-23 15:15

No, os.walk can't be used in this way.

查看更多
太酷不给撩
3楼-- · 2019-02-23 15:19

This looks like a usable walk-function. Currently not tested by me. https://pysftp.readthedocs.org/en/release_0.2.8/pysftp.html#pysftp.Connection.walktree

查看更多
登录 后发表回答