In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to get the file size in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
#! / usr/bin/python3.4
#-*-coding: utf-8-*-
Import os
# byte bytes converts kb\ m\ g
Def formatSize (bytes):
Try:
Bytes = float (bytes)
Kb = bytes / 1024
Except:
Print ("the incoming bytes are not in the correct format")
Return "Error"
If kb > = 1024:
M = kb / 1024
If M > = 1024:
G = M / 1024
Return "% f G"% (G)
Else:
Return "% f M"% (M)
Else:
Return "f kb"% (kb)
# get file size
Def getDocSize (path):
Try:
Size = os.path.getsize (path)
Return formatSize (size)
Except Exception as err:
Print (err)
# get folder size
Def getFileSize (path):
Sumsize = 0
Try:
Filename = os.walk (path)
For root, dirs, files in filename:
For fle in files:
Size = os.path.getsize (path + fle)
Sumsize + = size
Return formatSize (sumsize)
Except Exception as err:
Print (err)
If _ name__ = = "_ _ main__":
Print (getDocSize (".. / detailhtml/20161103112313.html"))
# 1006.142578kb
Print (getFileSize (".. / data/"))
# 111.856756M
On how to get the file size in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.