I have a script that creates and tmp directory on an SFTP server and then puts files in said /tmp
once the transfer is complete however I need to move the files from /tmp
back one directory to root /
. Use Paramiko how would I move the files from one remote directory to another?
Step guide:
Local files -----> Remote Temporary Dir ----> Remote root Dir
Code below if needed:
#!/usr/bin/python
# --------------------------------------------------------------------
#import libraries
# --------------------------------------------------------------------
import paramiko as PM
import os
import datetime
# --------------------------------------------------------------------
# Global Variables
# --------------------------------------------------------------------
host = 'host IP address'
port = 22
username = 'Username'
password = '*********'
# Variable Paths
localPath = '/shares/MILKLINK/fromML'
remotePath = '/'
logPath = '/shares/MILKLINK/logs/PPcfg02.log'
SRCfiles = '/shares/MILKLINK/Milklink.cpy'
# --------------------------------------------------------------------
# Create LOG FILE
# --------------------------------------------------------------------
log = open(logPath, 'a')
log.write(datetime.datetime.now().isoformat()+'
')
# Creating lockfile
if(os.path.isfile('LockSFTP')):
log.write("LOCK FILE STILL EXISTS!")
quit()
else:
os.system(">LockSFTP")
# --------------------------------------------------------------------
# Remove all files from /formML/
# --------------------------------------------------------------------
fileList = os.listdir(localPath)
for fileName in fileList:
try:
os.remove(localPath+"/"+fileName)
except OSError:
log.write("%s could not be deleted
" % fileName)
# --------------------------------------------------------------------
# Create SFTP CONNECTION
# --------------------------------------------------------------------
log.write("Starting Connection...
")
# SSH connection
ssh_Connection = PM.Transport((host, port))
ssh_Connection.connect(username = username, password = password)
# Creaat SFTP CLIENT SERVICES
sftp = PM.SFTPClient.from_transport(ssh_Connection)
log.write("Connection Established...
")
remoteList = sftp.listdir(remotePath)
fileList = os.listdir(SRCfiles)
try:
sftp.chdir(remotePath+'/tmp')
except IOError:
sftp.mkdir(remotePath+'/tmp')
sftp.chdir(remotePath+'/tmp')
for fileName in fileList:
if 'comphaulier.asc' not in remoteList:
if 'Last' in fileName:
continue
else:
sftp.put(SRCfiles+'/'+fileName, remotePath+'/tmp/'+fileName)
log.write(fileName+" Transferred
")
else:
log.write("Files Still Exist
")
log.close()
quit()
checkList = sftp.listdir(remotePath)
if len(checkList) == 7:
sftp.put(SRCfiles+'/LastFile.lst', remotePath+'/LastFile.lst')
log.write("LastFile.lst Transferred
")
else:
log.write("Not all files transferred!!!
")
quit()
sftp.close()
ssh_Connection.close()
os.system("rm LockSFTP")
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…