FTP上传文件Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17438096/
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
FTP upload files Python
提问by misguided
I am trying to upload file from windows server to a unix server (basically trying to do FTP). I have used the code below
我正在尝试将文件从 Windows 服务器上传到 unix 服务器(基本上是尝试执行 FTP)。我使用了下面的代码
#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\windows\folder\which\has\file")
ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)
I am getting the following error:
我收到以下错误:
Traceback (most recent call last):
File "Windows\folder\which\has\file\MyFile.py", line 11, in <module>
ftp.storbinary('RETR %s' % filename, open(filename, 'w').write)
File "windows\folder\Python\lib\ftplib.py", line 466, in storbinary
buf = fp.read(blocksize)
AttributeError: 'builtin_function_or_method' object has no attribute 'read'
Also all contents of MyFile.py
got deleted .
的所有内容也MyFile.py
被删除了。
Can anyone advise what is going wrong.I have read that ftp.storbinary is used for uploading files using FTP.
谁能告诉我出了什么问题。我读过 ftp.storbinary 用于使用 FTP 上传文件。
采纳答案by John
If you are trying to store a non-binary file(like a text file) try setting it to read mode instead of write mode.
如果您尝试存储非二进制文件(如文本文件),请尝试将其设置为读取模式而不是写入模式。
ftp.storlines("STOR " + filename, open(filename, 'r'))
for a binary file(anything that cannot be opened in a text editor) open your file in read-binary mode
对于二进制文件(任何无法在文本编辑器中打开的文件)以读取二进制模式打开文件
ftp.storbinary("STOR " + filename, open(filename, 'rb'))
also if you plan on using the ftp lib you should probably go through a tutorial, I'd recommend this articlefrom effbot.
另外,如果您打算使用 ftp lib,您可能应该阅读教程,我会推荐effbot 的这篇文章。
回答by mbdavis
try making the file an object, so you can close it at the end of the operaton.
尝试使文件成为对象,以便您可以在操作结束时关闭它。
myfile = open(filename, 'w')
ftp.storbinary('RETR %s' % filename, myfile.write)
and at the end of the transfer
并在传输结束时
myfile.close()
this might not solve the problem, but it may help.
这可能无法解决问题,但可能会有所帮助。
回答by misguided
Combined both suggestions. Final answer being
结合这两个建议。最终答案是
#!/usr/bin/python
import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
ftp.cwd("/Unix/Folder/where/I/want/to/put/file")
os.chdir(r"\windows\folder\which\has\file")
myfile = open(filename, 'r')
ftp.storlines('STOR ' + filename, myfile)
myfile.close()
回答by gbonetti
ftplibsupports the use of context managersso you can make it even simpler as such
ftplib支持使用上下文管理器,因此您可以使其更简单
with ftplib.FTP('ftp_address', 'user', 'pwd') as ftp, open(file_path, 'rb') as file:
ftp.storbinary(f'STOR {file_path.name}', file)
...
This way you are robust against both file and ftp issues without having to insert try/except/finally blocks. And well, it's pythonic.
通过这种方式,您无需插入 try/except/finally 块即可应对文件和 ftp 问题。好吧,它是pythonic。
PS: since it uses f-strings is python >= 3.6 only but can easily be modified to use the old .format() syntax
PS:因为它使用 f-strings 只是 python >= 3.6 但可以很容易地修改为使用旧的 .format() 语法