How to check for remaining hard drive space in Windows?

K

kevinliu23

HI,

I am new to Python and wanted to know how to check for the remaining
disk space on my Windows machine using Python? I was thinking of using
the command line "dir" and trying to extract the output from there.
But I'm not sure how to extract command line strings using Python
either.

Anyway help would be appreciated. :)
 
T

Tim Golden

[... re getting free disk space ...]

Sick said:
Here you are:

... print disk.Name, disk.FreeSpace
...
A: None
C: 16978259968
D: None

Well it's not often someone beats me to a WMI
solution :) Just to be different, you can also
look at the GetDiskFreeSpace function in the
win32api module of the pywin32 extensions.

The doc says: """
tuple = GetDiskFreeSpace(rootPath)

Retrieves information about the specified disk, including the amount of
free space available.

Parameters

rootPath : string

Specifies the root directory of the disk to return information about. If
rootPath is None, the method uses the root of the current directory.

Win32 API References

Search for GetDiskFreeSpace at msdn, google or google groups.

Return Value
The return value is a tuple of 4 integers, containing the number of
sectors per cluster, the number of bytes per sector, the total number of
free clusters on the disk and the total number of clusters on the disk.
If the function fails, an error is returned.
"""


TJG
 
K

kevinliu23

Thanks so much for the help guys. I got the code Sick Monkey provided
to work on my computer. Now I"m more confused than ever though. :) I
thought the only standard modules provided by Python are listed here:

http://docs.python.org/modindex.html

But it appears that there are other modules available to me without
having to download third party code. Could someone point me to the
documentation of these other modules such as win32com.client? Thanks
everyone for your help. :)

Also, how could I get the computer name? I would be running this code
on another machine and don't want to change the computer name in the
code every time I port it to another computer. Thanks!

Kevin

[... re getting free disk space ...]

Sick said:
Here you are:
... print disk.Name, disk.FreeSpace
...
A: None
C: 16978259968
D: None

Well it's not often someone beats me to a WMI
solution :) Just to be different, you can also
look at the GetDiskFreeSpace function in the
win32api module of the pywin32 extensions.

The doc says: """
tuple = GetDiskFreeSpace(rootPath)

Retrieves information about the specified disk, including the amount of
free space available.

Parameters

rootPath : string

Specifies the root directory of the disk to return information about. If
rootPath is None, the method uses the root of the current directory.

Win32 API References

Search for GetDiskFreeSpace at msdn, google or google groups.

Return Value
The return value is a tuple of 4 integers, containing the number of
sectors per cluster, the number of bytes per sector, the total number of
free clusters on the disk and the total number of clusters on the disk.
If the function fails, an error is returned.
"""

TJG
 
K

kevinliu23

Just tried your solution Tim, worked like a charm. :)

It's great because I don't even have to worry about the computer name.
A question regarding the rootPath parameter...how would I be passing
it? Would I be passing it as...

tuple = win32api.GetDiskFreeSpace(r'C:')
or just leave it blank and the function will automatically use the
rootPath of where the .py file resides?

Both have returned the correct result.

Kevin

Thanks so much for the help guys. I got the code Sick Monkey provided
to work on my computer. Now I"m more confused than ever though. :) I
thought the only standard modules provided by Python are listed here:

http://docs.python.org/modindex.html

But it appears that there are other modules available to me without
having to download third party code. Could someone point me to the
documentation of these other modules such as win32com.client? Thanks
everyone for your help. :)

Also, how could I get the computer name? I would be running this code
on another machine and don't want to change the computer name in the
code every time I port it to another computer. Thanks!

Kevin

[... re getting free disk space ...]
Well it's not often someone beats me to a WMI
solution :) Just to be different, you can also
look at the GetDiskFreeSpace function in the
win32api module of the pywin32 extensions.
The doc says: """
tuple = GetDiskFreeSpace(rootPath)
Retrieves information about the specified disk, including the amount of
free space available.

rootPath : string
Specifies the root directory of the disk to return information about. If
rootPath is None, the method uses the root of the current directory.
Win32 API References
Search for GetDiskFreeSpace at msdn, google or google groups.
Return Value
The return value is a tuple of 4 integers, containing the number of
sectors per cluster, the number of bytes per sector, the total number of
free clusters on the disk and the total number of clusters on the disk.
If the function fails, an error is returned.
"""
 
T

Tim Golden

kevinliu23 said:
Thanks so much for the help guys. I got the code Sick Monkey provided
to work on my computer. Now I"m more confused than ever though. :) I
thought the only standard modules provided by Python are listed here:

http://docs.python.org/modindex.html

But it appears that there are other modules available to me without
having to download third party code. Could someone point me to the
documentation of these other modules such as win32com.client? Thanks
everyone for your help. :)

Chances are you're running the ActiveState distro
of Python. Either that or you downloaded the pywin32
extensions in your sleep:

http://pywin32.sf.net

In any case, that's where the win32com.client and
friends come from. The installation normally supplies
a handy .chm file which gives all the docs.
Also, how could I get the computer name?

You can do that with WMI as well. Just look at
the Win32_ComputerSystem object and its Caption
attribute:

http://msdn2.microsoft.com/en-us/library/aa394102.aspx

TJG
 
J

Jerry Hill

Well it's not often someone beats me to a WMI
solution :) Just to be different, you can also
look at the GetDiskFreeSpace function in the
win32api module of the pywin32 extensions.

The MSDN page for that function warns:
"The GetDiskFreeSpace function cannot report volume sizes that are
greater than 2 gigabytes (GB). To ensure that your application works
with large capacity hard drives, use the GetDiskFreeSpaceEx function."

Make sure to keep that in mind if you're recording free disk space
someplace, rather than just checking for enough space to do an install
or something.
 
K

kevinliu23

Hmmmm, right now...I'm doing multiplication on the index values
returned by GetDiskFreeSpace. According to the documentation...

tuple[0]: sectors per cluster
tuple[1]: number of bytes per sector
tuple[2]: total number of free clusters
tuple[3]: total number of clusters on the disk

So I'm multiplying together indices 0, 1 and 2 together to get the
amount of free space left on my hard drive in bytes. The product of
the first three indices is over 10 gigabytes and is correct according
to my calculations. Why would the documentation say it does not return
data over 2 gigabytes then? Or do you mean not over 2 gigabytes
returned for tuple[2]? I believe my tuple[2] returned a value of
2778727 bytes, which is well below the 2 gigabyte capacity.

Anyway, thanks for letting me know about GetDiskFreeSpaceEx. I will
look into this function as well.

Thanks guys!
 
T

Tim Golden

kevinliu23 said:
Just tried your solution Tim, worked like a charm. :)

It's great because I don't even have to worry about the computer name.
A question regarding the rootPath parameter...how would I be passing
it? Would I be passing it as...

tuple = win32api.GetDiskFreeSpace(r'C:')
or just leave it blank and the function will automatically use the
rootPath of where the .py file resides?

Both have returned the correct result.

The simple answer is: I'm not sure. If you experiment and find
something which works, just use it!

Something which SickMonkey made me return to your question.
Are you trying to find the disk space available on a
different machine (possibly on several different machines)?
If so, then WMI is definitely your answer. You can
run -- from your machine -- one piece of code which
will attach to several different machines to give
you the answer. (as per Sick Monkey's later post).

If I've read too much into your question, well nothing's
ever wasted on the internet. Here's some sample code
which uses the wmi module from:

http://timgolden.me.uk/python/wmi.html

<code>
machines = ['mycomp', 'othercomp']

for machine in machines:
print "Machine:", machine
c = wmi.WMI (machine)
# only consider local fixed disks
for disk in c.Win32_LogicalDisk (DriveType=3):
print disk.Name, disk.FreeSpace
print

</code>

Yes, you could even write:

for disk in wmi.WMI (machine).Win32_LogicaDisk (DriveType=3):

but I'd find it a touch unwieldy. YMMV.

HTH
TJG
 
G

Gabriel Genellina

It's great because I don't even have to worry about the computer name.
A question regarding the rootPath parameter...how would I be passing
it? Would I be passing it as...

tuple = win32api.GetDiskFreeSpace(r'C:')
or just leave it blank and the function will automatically use the
rootPath of where the .py file resides?

For GetDiskFreeSpace, the argument *must* end in \, so you should use
GetDiskFreeSpace('C:\\')
Using GetDiskFreeSpaceEx, it can be any directory. If you leave it, the
current directory (or current disk) is used - this may or may not be the
directory where the .py resides.
About the 2GB limit, it only applies to Win98 and earlier. Since the ...Ex
function works on 98 too, unless you need to support Win95, it's easier to
use that function.
And you can use UNC paths too, so it may even be used for querying
available space on remote machines, but I've never tried it that way.
 
K

kevinliu23

Thanks Tim,

I only need to run the get hard drive space function on one drive for
one machine so I'll stick to GetDiskFreeSpace. If I need to expand
this feature to multiple harddrives/machines, I'll be sure to come
back to this thread. :)

Kevin

kevinliu23 said:
Just tried your solution Tim, worked like a charm. :)
It's great because I don't even have to worry about the computer name.
A question regarding the rootPath parameter...how would I be passing
it? Would I be passing it as...
tuple = win32api.GetDiskFreeSpace(r'C:')
or just leave it blank and the function will automatically use the
rootPath of where the .py file resides?
Both have returned the correct result.

The simple answer is: I'm not sure. If you experiment and find
something which works, just use it!

Something which SickMonkey made me return to your question.
Are you trying to find the disk space available on a
different machine (possibly on several different machines)?
If so, then WMI is definitely your answer. You can
run -- from your machine -- one piece of code which
will attach to several different machines to give
you the answer. (as per Sick Monkey's later post).

If I've read too much into your question, well nothing's
ever wasted on the internet. Here's some sample code
which uses the wmi module from:

http://timgolden.me.uk/python/wmi.html

<code>
machines = ['mycomp', 'othercomp']

for machine in machines:
print "Machine:", machine
c = wmi.WMI (machine)
# only consider local fixed disks
for disk in c.Win32_LogicalDisk (DriveType=3):
print disk.Name, disk.FreeSpace
print

</code>

Yes, you could even write:

for disk in wmi.WMI (machine).Win32_LogicaDisk (DriveType=3):

but I'd find it a touch unwieldy. YMMV.

HTH
TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top