misguided misguided - 1 year ago 157
Python Question

FTP upload files 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

import ftplib
import os
filename = "MyFile.py"
ftp = ftplib.FTP("xx.xx.xx.xx")
ftp.login("UID", "PSW")
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
got deleted .

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

Answer Source

If you are trying to store a file try setting it to read mode instead of write mode.

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

also if you plan on using the ftp lib you should probably go through a tutorial, I'd recommend this article from effbot.