Get folder size using FtpLib

前端 未结 3 1110
说谎
说谎 2021-01-03 11:17

I\'m using FtpLib (Python) to download a folder with files from an ftp server. But I want to know the size of the folder before I start downloading it.

In the docume

3条回答
  •  别那么骄傲
    2021-01-03 11:56

    After some trial and error I found the solution.

    It's based on Alex Thorntons solution but I kept in mind that there could be subfolders. When it's a folder, the ftp.cwd() command succeeds and the function is called recursively.

    When the ftp.cwd() command throws an error, it was a file and we can get the file size. The type is set to "I" (images), this forces binary mode since the ftp.size() command is not allowed in ASCII mode.

    def get_total_size(self, directory):
        size = 0
        for filename in self.ftp.nlst(directory):
            try:
                self.ftp.cwd(filename)
                size += self.get_total_size(filename)
            except:
                self.ftp.voidcmd('TYPE I')
                size += self.ftp.size(filename)
        return size
    

提交回复
热议问题