mirror of
				https://github.com/KevinMidboe/Node-Com-Handler.git
				synced 2025-10-29 17:50:27 +00:00 
			
		
		
		
	Now allows a optional path to be checked, and added some error handling if directory doesn't exist
This commit is contained in:
		| @@ -3,13 +3,14 @@ | |||||||
| # @Author: KevinMidboe | # @Author: KevinMidboe | ||||||
| # @Date:   2017-01-28 10:54:06 | # @Date:   2017-01-28 10:54:06 | ||||||
| # @Last Modified by:   KevinMidboe | # @Last Modified by:   KevinMidboe | ||||||
| # @Last Modified time: 2017-01-31 21:40:34 | # @Last Modified time: 2017-01-31 22:29:43 | ||||||
|  |  | ||||||
| # f_avail = free blocks avail to non sudo | # f_avail = free blocks avail to non sudo | ||||||
| # frsize = fundamental file sys byte size | # frsize = fundamental file sys byte size | ||||||
| # f_blocks = total number of blocks in the filesystem | # f_blocks = total number of blocks in the filesystem | ||||||
|  |  | ||||||
| from os import statvfs | from os import statvfs | ||||||
|  | from re import sub | ||||||
|  |  | ||||||
| def sizeof(num, suffix='B'): | def sizeof(num, suffix='B'): | ||||||
| 	for unit in ['','K','M','G','T','P','E','Z']: | 	for unit in ['','K','M','G','T','P','E','Z']: | ||||||
| @@ -19,7 +20,11 @@ def sizeof(num, suffix='B'): | |||||||
| 	return "%.1f%s%s" % (num, 'Y', suffix) | 	return "%.1f%s%s" % (num, 'Y', suffix) | ||||||
|  |  | ||||||
| # diskUsage uses statvfs to calculate disk size and availability for given disk | # diskUsage uses statvfs to calculate disk size and availability for given disk | ||||||
| def diskUsage(path='/'): | def diskUsage(optionalPath=None): | ||||||
|  | 	path = '/' | ||||||
|  | 	if optionalPath != None: | ||||||
|  | 		path += sub('\+', '/', optionalPath) | ||||||
|  |  | ||||||
| 	try: | 	try: | ||||||
| 		s = statvfs(path) | 		s = statvfs(path) | ||||||
| 		byteLeft = s.f_bavail * s.f_frsize | 		byteLeft = s.f_bavail * s.f_frsize | ||||||
| @@ -30,6 +35,8 @@ def diskUsage(path='/'): | |||||||
|  |  | ||||||
| 	except FileNotFoundError: | 	except FileNotFoundError: | ||||||
| 		return None | 		return None | ||||||
|  | 	except TypeError: | ||||||
|  | 		return None | ||||||
|  |  | ||||||
| if __name__=="__main__": | if __name__=="__main__": | ||||||
| 	n = diskUsage('/media/hdd1') | 	n = diskUsage('/media/hdd1') | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user