FTP status problems. (Again)

marduk marduk+news at letterboxes.org
Sat Sep 17 00:58:57 EDT 2005


On Sat, 2005-09-17 at 04:42 +0000, marduk wrote:
> 
> ... and I haven't tried this myself, but you should be able to subclass
> the builtin file object and prepare your own read() method.  Something
> like
> 
> class ProgressFile(file):
> 
>     def read(self, size = None):
>         print '.',
> 
>         if size is not None:
>             return file.read(self, size)
>         else:
>             return file.read()
> 
> May need some tweaking.. then store the file as
> 
> ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)
> 
> Give it a try..
> 
> -m
> 

I corrected some errors and made some modifications to my previous post:

class ProgressFile(file):
    def read(self, size = None):
        from sys import stdout

        if size is not None:
            buff = file.read(self, size)
            if buff:
                stdout.write('.')
            else:
                stdout.write('\n')
            return buff
        else:
            buff = ''
            while True:
                new_str = file.read(self, 1024)
                stdout.write('.')
                if new_str:
                    buff = buff + new_str
                else:
                    stdout.write('\n')
                    break
            return buff


if __name__ == '__main__':
    import sys

    fname = sys.argv[1]
    f = ProgressFile(fname)
    f.read()





More information about the Python-list mailing list