使用 Paramiko 将文件从一个目录移动到另一个目录 [英] Move files from one directory to another with Paramiko
问题描述
我有一个脚本,可以在 SFTP 服务器上创建 tmp 目录,然后在传输完成后将文件放入 /tmp
中,但是我需要从 /tmp<移动文件/code> 返回一个目录到根
/
.使用 Paramiko 如何将文件从一个远程目录移动到另一个目录?
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?
步骤指南:
本地文件----->远程临时目录---->远程根目录
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")
推荐答案
使用 sftp.rename
:
sftp.rename(remotePath+'/tmp/'+fileName, remotePath+fileName)
请注意,如果源目录和目标目录位于不同的文件系统上,则某些 SFTP 服务器会导致请求失败.
Note that some SFTP servers fail the request, if the source and target directories are on different file systems.
如果您需要将一组文件从一个文件夹移动到另一个文件夹,请参阅:
使用 Python 将所有文件从一个 SFTP 文件夹归档到另一个文件夹
If you need to move set of files from one folder to another, see:
Archive all files from one SFTP folder to another in Python
这篇关于使用 Paramiko 将文件从一个目录移动到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!