Python 官方文档:入门教程 => 点击学习
【背景】 本程序遍历 ftp 目录,列出单个文件大小,统计目录个数、文件个数、文件总大小。目的是在批量下载 FTP 文件时,不严格的验证下载结果的正确性。 【环境】 windows10 下 python 3.6.5,第三方包 f
【背景】
本程序遍历 ftp 目录,列出单个文件大小,统计目录个数、文件个数、文件总大小。目的是在批量下载 FTP 文件时,不严格的验证下载结果的正确性。
【环境】
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'ftp.ncbi.nlm.nih.Gov' # FTP 主机
SubDir = r'/pubmed/baseline/' # 最后的斜线有无不影响,根目录用单斜线即可
FtpUser = r'anonymous'
FtpPwd = r''
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(SubDir):
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 ***
--结束END--
本文标题: Python3 统计 ftp 文件个数和
本文链接: https://www.lsjlt.com/news/192874.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0