Using Python to print the amount of space left in a network shared drive
Edited following comment
import ctypes, os, pythoncom
from win32com.client import Dispatch
def drivespace(drive, xl_path, col):
#get space in bytes
free_bytes = ctypes.c_ulonglong(0)
ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(drive), \
None, None, ctypes.pointer(free_bytes))
#calculate space in GB
free_GB = free_bytes.value/1024/1024/1024
print(free_GB)
#input data to Excel
xl = Dispatch('Excel.Application')
xl.visible = 0
wb = xl.Workbooks.Open(xl_path)
ws = wb.Worksheets(1)
# initialise values
empty = False
row = 1
# loop until first empty cell in this column
while not empty:
val = ws.Range(col+str(row)).value
print val
if val == None:
print "Found first empty cell! Writing Value..."
ws.Range(col+str(row)).value = free_GB
empty = True
row += 1
wb.Close(True)
xl.Quit()
#release held Excel process
pythoncom.CoUninitialize()
def main():
drive = 'C:\\'
xl_path = os.path.join(os.getenv('so'),'free_space_to_excel','Book1.xlsm')
xl_column = 'A'
drivespace(drive, xl_path, xl_column)
if __name__ == '__main__':
main()
You will just need to change the values in the main procedure to set them to your drive, xl_path etc. This takes an additional arguement for a column letter and finds the first available cell in that column. I think this is a safer approach than relying on a particular cell being active when you open the sheet.
How can I check how much free space there is in a disk with Python?
You could use the os
library in python to execute shell commands. For windows, command to check directory usage is fsutil volume diskfree c:
so you could do something like:
import os
command = "fsutil volume diskfree c:"
os.system(command)
Hope this works! The output should be something like:
Total # of free bytes : 145709916160
Total # of bytes : 254930841600
Total # of avail free bytes : 145709916160
Cross platform way to list disk drives on Linux, Windows and Mac using Python?
There isn't really a unified naming scheme for Linux devices that guarantees you a formatable block device. There are conventions, but they can vary widely and I can call my thumb-drive /Thomas/O if I want and there is no cross-platform way in Python to know:
- That /Thomas/O corresponds to /dev/sdf1
- That /dev/sdf1 can have a FAT32 filesystem made on it
- That /dev/sdf is not preferred to /dev/sdf1
I'm pretty sure that neither is there a cross-platform Python module which will allow you to determine that H:/ is formattable on a Windows system but that Z:/ is not.
Each system will require its own specific checks and validations which you could best learn from studying open-source disk manipulation software.
Find free disk space in python on OS/X
Try using f_frsize
instead of f_bsize
.
>>> s = os.statvfs('/')
>>> (s.f_bavail * s.f_frsize) / 1024
23836592L
>>> os.system('df -k /')
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/disk0s2 116884912 92792320 23836592 80% /
Is there a way to list all the available Windows' drives?
import win32api
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
print drives
Adapted from:
http://www.faqts.com/knowledge_base/view.phtml/aid/4670
Related Topics
Pass Input/Variables to Command/Script Over Ssh Using Python Paramiko
Get Total Physical Memory in Python
Converting Epoch Time With Milliseconds to Datetime
Does Python Have a Ternary Conditional Operator
How to Generate All Permutations of a List
Limiting Floats to Two Decimal Points
Is It Pythonic to Use List Comprehensions For Just Side Effects
How to Concatenate Two Lists in Python
How Are Iloc and Loc Different
Python Pysftp Get_R from Linux Works Fine on Linux But Not on Windows
How Would I Build Python Myself from Source Code on Ubuntu
Integer Division by Negative Number
"Is" Operator Behaves Unexpectedly With Integers
How to Add to the Pythonpath in Windows, So It Finds My Modules/Packages
Error: Unable to Find Vcvarsall.Bat