I am working on server 1
. I need to write a Python script where I need to connect to a server 2
and get certain files (files whose name begins with the letters 'HM') from a directory and put them into another directory, which needs to be created at the run time (because for each run of the program, a new directory has to be created and the files must be dumped in there), on server 1
.
I need to do this in Python and I'm relatively new to this language. I have no idea where to start with the code. Is there a solution that doesn't involve 'tarring' the files? I have looked through Paramiko but that just transfers one file at a time to my knowledge. I have even looked at glob but I cannot figure out how to use it.
In this article, we will discuss how to copy all files from one directory to another using Python. This can be done using the shutil module. This module can be used in Python to perform operations on files and folders in a directory. Shutil package facilitates the access, movement, and removal of the files between directories.
How to copy a file to a remote server in Python using SCP or SSH? The easiest way to copy files from one server to another over ssh is to use the scp command. For calling scp you'd need the subprocess module. You need the waitpid call to wait for the copying to complete.
The steps to copy files from one server to another server are: Step 1: Get login information for each server. Step 2: Get file path of the files to be copied. Step 3: Login to the second server and use scp command to copy files. The detailed steps are as follows: Step 1: Get login information for each server.
Python Get Files In Directory – Getting Files With Pathlib Module 1 pathlib module offers classes representing filesystem paths with semantics appropriate for different operating systems. 2 Python 3.2 or later is recommended, but pathlib is also usable with Python 2.7 and 2.6. 3 So let’s see how can we do directory listing using pathlib module.
to transfer the files you might wanna check out paramiko
import os
import paramiko
localpath = '~/pathNameForToday/'
os.system('mkdir ' + localpath)
ssh = paramiko.SSHClient()
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
ssh.connect(server, username=username, password=password)
sftp = ssh.open_sftp()
sftp.get(remotepath, localpath)
sftp.close()
ssh.close()
I you wanna use glob you can do this:
import os
import re
import glob
filesiwant = re.compile('^HM.+') #if your files follow a more specific pattern and you don't know regular expressions you can give me a sample name and i'll give you the regex4it
path = '/server2/filedir/'
for infile in glob.glob( os.path.join(path, '*') ):
if filesiwant.match(infile):
print "current file is: " + infile
otherwise an easier alternative is to use os.listdir()
import os
for infile in os.listdir('/server2/filedir/'):
...`
does that answer your question? if not leave comments
Python wouldn't be my first choice for this task, but you can use calls to the system and run mkdir and rsync. In particular you could do
import os
os.system("mkdir DIRECTORY")
os.system("rsync -cav user@server2:/path/to/files/HM* DIRECTORY/")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With