【環境】
Windows10 下 Python 3.6.5,第三方包 ftputil 3.4。
【ftp_stat】
# encoding: utf-8 # author: walker # date: 2018-10-12 # summary: 遍歷 ftp 目錄,列出單個文件大小,統計目錄個數、文件個數、文件總大小。 import time import ftputil FtpHost = r'192.168.xx.xx' # FTP 主機 FtpUser = r'ftpadmin' FtpPwd = r'password' FtpEncoding = r'utf-8' def Main(): r""" 遍歷 ftp 目錄,列出單個文件大小,統計目錄個數、文件個數、文件總大小。 """ fileCnt = 0 fileSize = 0 dirCnt = 0 with ftputil.FTPHost(host=FtpHost, user=FtpUser, passwd=FtpPwd) as host: for parent, dirnames, filenames in host.walk('/'): for filename in filenames: fileCnt += 1 pathfile = host.path.join(parent, filename) singleFileSize = host.path.getsize(pathfile) fileSize += singleFileSize print('\tfile: %s, %d bytes' % (pathfile.encode('latin-1').decode(FtpEncoding), singleFileSize)) for dirname in dirnames: dirCnt += 1 pathdir = host.path.join(parent, dirname) print('\tdir: %s' % pathdir.encode( 'latin-1').decode(FtpEncoding)) print('fileCnt: %d, fileSize: %d B/%.2f KB/%.2f MB/%.2f GB, dirCnt: %d' % (fileCnt, fileSize, fileSize/1024, fileSize/1024/1024, fileSize/1024/1024/1024, dirCnt)) print('fileCnt: %d, fileSize: %d B/%.2f KB/%.2f MB/%.2f GB, dirCnt: %d' % (fileCnt, fileSize, fileSize/1024, fileSize/1024/1024, fileSize/1024/1024/1024, dirCnt)) if __name__ == '__main__': Main() print('current time: %s\n' % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
【相關閱讀】
*** walker ***