Python Forum
Looping Through Linux Filesystems
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping Through Linux Filesystems
#1
Hello,

I'm new to Python and using 3.6 for this code. I wrote the code below to report on a Linux filesystem called '/u01'. What I'd like to do is loop through the absolute filesystems and print out the size, free space, etc for each of the filesystems such as '/','/backup','/var', etc.

Here is my current piece of code:
import subprocess
import os, sys
from collections import namedtuple

def get_path_usage(path):
	
	space_st = os.statvfs(path)

	# f_bavail: without blocks reserved for super users
	# f_bfree: with blocks reserved for super users

	avail = space_st.f_frsize * space_st.f_bavail

	capa = space_st.f_frsize * space_st.f_blocks
	used = round((capa - avail) / 1024 / 1024)
	percent = float(used) / capa

	# print using str.format(python 3) method for variables
	print ("Path: {}".format(path))
	print ("Total: {}".format(capa))
	print ("Used: {}".format(used))
	print ("Available: {}".format(avail))
	print ("Percent: {}".format(percent))

x = get_path_usage('/u01')
print (x)
I'd like to know how to add another function to loop through directories, or even add the directories to a list, to report on their size. I've been looking around for a while and decided to ask the experts with this post.

Appreciate the help!

Frank
Reply


Messages In This Thread
Looping Through Linux Filesystems - by anelliaf - Mar-02-2018, 07:44 PM
RE: Looping Through Linux Filesystems - by Larz60+ - Mar-02-2018, 07:48 PM
RE: Looping Through Linux Filesystems - by anelliaf - Mar-05-2018, 06:59 PM
RE: Looping Through Linux Filesystems - by wavic - Mar-05-2018, 08:20 PM
RE: Looping Through Linux Filesystems - by anelliaf - Mar-06-2018, 03:27 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020