3

How to copy a folder from Server (linux) to a local machine (windows) in python.

I tried with the given code but it did not work

from distutils.dir_util import copy_tree
copy_tree("source_path ","destination_path")

I used copy_tree command to copy a folder on my local machine but when I used the same command to copy a folder from server to local machine then it did not work.

Any other method is there? Or any changes needed?

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
python
  • 83
  • 1
  • 16

2 Answers2

4

You need to use SSH, SCP, or SFTP to transfer files from host to host.

I do this a lot and like to use SSH and SCP. You can run and SSH server on your windows machine using OpenSSH. Here is a good set of instructions from WinSCP: https://winscp.net/eng/docs/guide_windows_openssh_server.

I recommend using Paramiko for SSH with Python. Here is a good answer showing how this works with python: https://stackoverflow.com/a/38556344/634627.

If you set up OpenSSH, you could also do this with SFTP, sometimes I find this is more suitable that SCP. Here is a good answer showing how that works: https://stackoverflow.com/a/33752662/634627

The trick is getting OpenSSH running on your Windows host and setting up SSH keys so your server can authenticate to your localhost.

Mike
  • 2,514
  • 2
  • 23
  • 37
1

Using copytree should work if:

  1. the folder on the server is made available to the windows machine as a client.
  2. you have sufficient access permissions.
  3. you use a raw string for the windows path to prevent string interpretation.

Ad 3: try print('c:\test\robot'):

In [1]: print('c:\test\robot')
obot    est
Roland Smith
  • 42,427
  • 3
  • 64
  • 94