gaviscon_man gaviscon_man - 1 year ago 138
Python Question

How do I copy a directory to a remote machine using Fabric?

I have a directory on my local machine that I would like to copy to a remote machine (and rename it) using Fabric. I know I can copy file using

, but what about a directory. I know it's easy enough using scp, but I would prefer to do it from within my
if possible.

Answer Source

You can use put for that as well (at least in 1.0.0):

local_path may be a relative or absolute local file or directory path, and may contain shell-style wildcards, as understood by the Python glob module. Tilde expansion (as implemented by os.path.expanduser) is also performed.


Update: This example works fine (for me) on 1.0.0.:

from fabric.api import env
from fabric.operations import run, put

env.hosts = ['']

def copy():
    # make sure the directory is there!
    run('mkdir -p /home/frodo/tmp')

    # our local 'testdirectory' - it may contain files or subdirectories ...
    put('testdirectory', '/home/frodo/tmp')

# [] Executing task 'copy'
# [] run: mkdir -p /home/frodo/tmp
# [] put: testdirectory/HELLO -> \
#     /home/frodo/tmp/testdirectory/HELLO
# [] put: testdirectory/WORLD -> \
#     /home/frodo/tmp/testdirectory/WORLD
# ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download