FTP上传文件Python [英] FTP upload files Python

查看:371
本文介绍了FTP上传文件Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文件从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)

我得到以下错误:

  Traceback(最近一次调用最后一次):
文件Windows \folder\which\has\file\MyFile.py,第11行,位于< module>
ftp.storbinary('RETR%s'%filename,open(filename,'w')。write)
文件windows \folder\Python\lib\ftplib.py,第466行,在storbinary
buf = fp.read(blocksize)
AttributeError:'builtin_function_or_method'对象没有属性'read'

也删除了 MyFile.py 的所有内容。



任何人都可以建议出了什么问题。我已经读过ftp.storbinary用于使用FTP上传文件。 如果您尝试存储非二进制文件(如文本文件),请尝试使用非二进制文件设置它为读模式而不是写模式。

  ftp.storlines(STOR+ filename,open(filename,'r '))

代表二进制文件(任何无法在一个文本编辑器)打开你的阅读二进制模式

  ftp.storbinary(STOR+ filename,open(filename,' rb'))

如果您计划使用ftp lib,您应该通过一个教程,我会推荐这个来自effbot的文章


I am trying to upload file from windows server to a unix server (basically trying to do FTP). I have used the code below

#!/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 .

Can anyone advise what is going wrong.I have read that ftp.storbinary is used for uploading files using FTP.

解决方案

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 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 article from effbot.

这篇关于FTP上传文件Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆