python ftp上传文件_FTP上传文件Python

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值