Python ftplib - 上传多个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1110374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python ftplib - uploading multiple files?
提问by Phil
I've googled but I could only find how to upload one file... and I'm trying to upload all files from local directory to remote ftp directory. Any ideas how to achieve this?
我用谷歌搜索过,但我只能找到如何上传一个文件......我正在尝试将所有文件从本地目录上传到远程 ftp 目录。任何想法如何实现这一目标?
回答by SilentGhost
with the loop?
与循环?
edit: in universal case uploading only files would look like this:
编辑:在通用情况下,仅上传文件如下所示:
import os
for root, dirs, files in os.walk('path/to/local/dir'):
for fname in files:
full_fname = os.path.join(root, fname)
ftp.storbinary('STOR remote/dir' + fname, open(full_fname, 'rb'))
Obviously, you need to look out for name collisions if you're just preserving file names like this.
显然,如果您只是像这样保留文件名,则需要注意名称冲突。
回答by Toulon7559
Look at Python-scriptlines required to make upload-files from JSON-Calland next FTPlib-operation: why some uploads, but others not?
查看从 JSON-Call和下一个FTPlib 操作制作上传文件所需的 Python脚本行:为什么有些上传,而有些不上传?
Although a different starting position than your question, in the Answerof that first url you see an example construction to upload by ftplib a json-file plus an xml-file: look at scriptline 024 and further.
尽管与您的问题的起始位置不同,但在第一个 url的答案中,您会看到一个示例结构,由 ftplib 上传一个 json 文件和一个 xml 文件:查看脚本行 024 及进一步。
In the second url you see some other aspects related to upload of more files.
在第二个 url 中,您会看到与上传更多文件相关的其他一些方面。
Also applicable for other file-types than json and xml, obviously with a different 'entry' before the 2 final sections which define and realize the FTP_Upload-function.
也适用于 json 和 xml 以外的其他文件类型,显然在定义和实现 FTP_Upload 功能的 2 个最后部分之前有一个不同的“条目”。
回答by blispr
Create a FTP batch file (with a list of files that you need to transfer). Use python to execute ftp.exe with the "-s" option and pass in the list of files.
创建一个 FTP 批处理文件(带有您需要传输的文件列表)。使用 python 执行带有“-s”选项的 ftp.exe 并传入文件列表。
This is kludgy but apparently the FTPlib does not have accept multiple files in its STOR command.
这很笨拙,但显然 FTPlib 在其 STOR 命令中没有接受多个文件。
Here is a sample ftp batch file.
这是一个示例 ftp 批处理文件。
*
*
OPEN inetxxx
myuser mypasswd
binary
prompt off
cd ~/my_reg/cronjobs/k_load/incoming
mput *.csv
bye
If the above contents were in a file called "abc.ftp" - then my ftp command would be
ftp -s abc.ftp
如果上述内容在名为“abc.ftp”的文件中 - 那么我的 ftp 命令将是
ftp -s abc.ftp
Hope that helps.
希望有帮助。