1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Copy directory and its contents

Discussão em 'Python' iniciado por Stack, Setembro 11, 2025.

  1. Stack

    Stack Membro Participativo

    In the below I'm copying a directory from a remote machine to local. Instead of copying the directory and its contents (files and directories), it's copying only the contents (files and directories). How do I make Paramiko SCP create a directory first and then copy the contents (files and directories) to it.

    main.py

    def main():
    ssh_client = paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    private_key = paramiko.RSAKey(filename="/home/harry/.ssh/id_rsa")
    ssh_client.connect(host_name,
    port = 22,
    username = user_name,
    private_key)

    scp = SCPClient(ssh_client.get_transport())
    scp.get(remote_path=f"/a/b/c/d/e/", local_path=f"./f/", recursive=True, preserve_times=True)
    scp.close()
    ssh_client.close()


    In the above SCP operation local directory f doesn't contain directory e copied from remote, instead it contains the contents (files/directories) of remote directory e.

    Continue reading...

Compartilhe esta Página