python 磁盤空間操作


python對磁盤操作

查看文件夾的大小

查看目錄掛載點的 使用情況

 
         
Disk_Usage = namedtuple("usage", "total used free percent")

class
DiskSpaceUtil(object): @staticmethod def get_dir_size(dir_path): """unit byte""" size = 0l for root, dirs, file_names in os.walk(dir_path): size += sum([os.path.getsize(os.path.join(root, file_name)) for file_name in file_names]) return size @staticmethod def get_dir_usage(dir_path): """The usage of this dir_path`s mount path""" """Return disk usage associated with path.""" st = os.statvfs(dir_path) free = (st.f_bavail * st.f_frsize) total = (st.f_blocks * st.f_frsize) used = (st.f_blocks - st.f_bfree) * st.f_frsize try: percent = ret = (float(used) / total) * 100 except ZeroDivisionError: percent = 0 return Disk_Usage(total, used, free, round(percent, 1))

References:

http://blog.csdn.net/magic_zj00/article/details/7207445


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM