简体   繁体   English

使用 Python 将本地服务器上一个目录中的多个文件传输到远程服务器上的不同目录

[英]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_1b.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转到一个目录,例如localb.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.

相关问题 在本地机器和远程服务器之间传输文件 - transfer files between local machine and remote server 使用python将文件从本地计算机传输到远程Windows服务器 - Transfer file from local machine to remote windows server using python 使用python将本地文件传输到远程服务器(窗口) - Transfer a local file to a remote server(windows) using python Python PySFTP将文件从一台远程服务器传输到另一台远程服务器 - Python PySFTP transfer files from one remote server to another remote server 将不同SFTP目录下的多个文件下载到本地 - Download multiple files in different SFTP directories to local 使用 Python,如何将 FTP 服务器上的子目录中的多个文件下载到本地计算机上的所需目录中? - Using Python, how to download multiple files from a subdirectory on FTP server into a desired directory on local machine? 如何删除Python远程SFTP服务器目录下的所有文件? - How to delete all files in directory on remote SFTP server in Python? 使用python将文件从服务器复制到本地目录 - To copy the files from a server to a local directory using python 在本地目录中的远程服务器上安装virtualenv - Installing virtualenv on a remote server in a local directory 从远程服务器遍历大型本地excel文件的目录,导致重复访问相同的文件 - Looping over a directory of large local excel files from a remote server, causes repeated access to same files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM