[英]Transfer multiple files in a directory on a local server to different directories on remote server in Python
There are some files inside a directory on an Informatica Server. Informatica 服务器上的目录中有一些文件。 I need to transfer these files onto another server based on their file names into different directories.
我需要根据它们的文件名将这些文件传输到另一台服务器到不同的目录中。
Say there are 2 files, a.dat_1
and b.dat_2
inside a directory called low
on the Informatica server.假设 Informatica 服务器上名为
low
的目录中有 2 个文件, a.dat_1
和b.dat_2
。
I need to transfer this low
directory onto another server where the file a.dat_1
goes to a directory say, local
and b.dat_2
goes to another directory called local2
.我需要将此
low
目录传输到另一台服务器上,其中文件a.dat_1
转到一个目录,例如local
而b.dat_2
转到另一个名为local2
目录。 This needs to be done in Python.这需要在 Python 中完成。
I have used Paramiko to do simple transfers but not directories.我使用 Paramiko 进行简单的传输,但不是目录。 And not in separate directories like local and local2.
而不是在像 local 和 local2 这样的单独目录中。
You can make a tar
archive out of your directory.您可以从您的目录中制作
tar
存档。 For this use shell command为此使用 shell 命令
tar -cvf archive_name.tar directory_name
then transfer this archive to another machine and untar
it there:然后将此存档传输到另一台机器并在那里
untar
:
tar -xvf archive_name.tar
You could also uze gzip or other compressor on your tarred archive to make the transfer faster.您还可以在 tarred 存档中使用 gzip 或其他压缩程序,以加快传输速度。
Given this SO Directory transfers on paramiko ;鉴于此 SO 目录在 paramiko 上传输; you won't be able to copy a directory as a bulk operation.
您将无法将目录复制为批量操作。
If it were me, I'd create a temp folder and start by copying the source files into a directory structure compatible with your destination requirements.如果是我,我会创建一个临时文件夹并首先将源文件复制到与您的目标要求兼容的目录结构中。 Then compress them as yakxxx suggests, send the compressed file over the wire with Paramiko SFTP and uncompress on the other end with Paramiko SSH.
然后按照yakxxx 的建议压缩它们,使用Paramiko SFTP 通过网络发送压缩文件,并在另一端使用 Paramiko SSH 解压缩。
I do something similar for transferring files daily.我每天都做类似的事情来传输文件。 As yakxxx suggests, I zip my files then transfer.
正如 yakxxx 建议的那样,我压缩文件然后传输。 Example of what I do (on Windows machines):
我所做的示例(在 Windows 机器上):
import zipfile
from glob import glob as gg
files = gg('path*.txt')
# open zip file (create it, or open if already exists)
zFile = zipfile.ZipFile('FileName.zip','w')
# zip files on local machine
[zFile.write(r,r,zipfile.ZIP_DEFLATED) for r in files]
Alternately, if the zipfile already exists and you want to add new files.或者,如果 zipfile 已经存在并且您想要添加新文件。
zFile = zipfile.ZipFile('FileName.zip','a')
# List files already zipped.
done = zipfile.ZipFile('FileName.zip','r').namelist()
# zip file into zip file.
[zFile.write(r,r,zipfile.ZIP_DEFLATED) for r in files if r not in done]
Now push this zip file to your remote machine via paramiko.现在通过 paramiko 将此 zip 文件推送到您的远程机器。
EDIT编辑
FYI, when writing to to zip files you need to be careful.仅供参考,在写入 zip 文件时,您需要小心。 I wrote this assuming you are running within the directory that has the files you want to zip.
我写这篇文章是假设您在包含要压缩的文件的目录中运行。 If you are not, you need to use:
如果不是,则需要使用:
import os
zFile.write(r,os.path.basename(r),zipfile.ZIP_DEFLATED)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.