views:

35

answers:

3

Hey all,
As far as i know it is impossible to create an empty file with ftp, you have to create an empty file on the local drive, upload it, then delete it when you are done. I was wondering if it is possible to do something like:

class FakeFile:
    def read(self):
        return '\x04'

ftpinstance.storbinary('stor fe', FakeFile())

I thought this might work because the docs for storbinary say that it takes an object with a method 'read' and calls it til it returns EOF, and \x04 is the ASCII EOF character. I have tried this though, and the file ends up on the server being a random number of kilobytes large. Am I misunderstanding something?

+1  A: 

End of File is not ASCII 4 in modern usage. That is obsolete usage from the days of XMODEM and teletype. Try this:

class FakeFile:
    def read(self, size=0):
        return ''

since an empty read is EOF.

With your current code, you should be getting a random length file consisting entirely of '\x04' characters. This is because it seems to always have data when read. ^_-

Mike DeSimone
Why does it ever finish uploading? Shouldn't it upload forever?
zeta
That I don't know. I'd think it should as well.
Mike DeSimone
A: 

Actually the doc reads 'reads until EOF'. I solved this by making the read function return an empty string.

zeta
+1  A: 
import ftplib
import cStringIO

ftp = ftplib.FTP('ftp.example.com')
ftp.login('user', 'pass')

voidfile = cStringIO.StringIO('')
ftp.storbinary('STOR emptyfile.foo', voidfile)

ftp.close()
voidfile.close()
Paulo Scardine