473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to check for remaining hard drive space in Windows?

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. :)

Feb 28 '07 #1
10 22555
[... re getting free disk space ...]

Sick Monkey wrote:
Here you are:
>>from win32com.client import GetObject
wmiObj = GetObject("winmgmts:\\\\MGW01641\\root\\cimv2")
diskinfo = wmiObj.ExecQuery("Select * from Win32_LogicalDisk")
for disk in diskinfo:
... 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
Feb 28 '07 #2
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

On Feb 28, 4:16 pm, Tim Golden <m...@timgolden.me.ukwrote:
[... re getting free disk space ...]

Sick Monkey wrote:
Here you are:
>>from win32com.client import GetObject
wmiObj = GetObject("winmgmts:\\\\MGW01641\\root\\cimv2")
diskinfo = wmiObj.ExecQuery("Select * from Win32_LogicalDisk")
for disk in diskinfo:
... 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

Feb 28 '07 #3
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

On Feb 28, 4:24 pm, "kevinliu23" <kevinli...@gmail.comwrote:
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

On Feb 28, 4:16 pm, Tim Golden <m...@timgolden.me.ukwrote:
[... re getting free disk space ...]
Sick Monkey wrote:
Here you are:
>>from win32com.client import GetObject
>>>wmiObj = GetObject("winmgmts:\\\\MGW01641\\root\\cimv2")
>>>diskinfo = wmiObj.ExecQuery("Select * from Win32_LogicalDisk")
>>>for disk in diskinfo:
... 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

Feb 28 '07 #4
kevinliu23 wrote:
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
Feb 28 '07 #5
On 2/28/07, Tim Golden <ma**@timgolden.me.ukwrote:
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.

--
Jerry
Feb 28 '07 #6
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!

On Feb 28, 5:08 pm, "Jerry Hill" <malaclyp...@gmail.comwrote:
On 2/28/07, Tim Golden <m...@timgolden.me.ukwrote:
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.

--
Jerry

Mar 1 '07 #7
kevinliu23 wrote:
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
Mar 1 '07 #8
En Wed, 28 Feb 2007 18:54:53 -0300, kevinliu23 <ke********@gmail.com>
escribió:
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.

--
Gabriel Genellina

Mar 1 '07 #9
ke********@gmail.com wrote:
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.
And, just for the record, there's even a few other
techniques outlined here:

http://aspn.activestate.com/ASPN/Coo...n/Recipe/66455

TJG

Mar 1 '07 #10
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

On Mar 1, 4:17 am, Tim Golden <m...@timgolden.me.ukwrote:
kevinliu23 wrote:
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

Mar 1 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ned Hart | last post by:
I'm formatting my mirrored drives and installing a third drive for raid 5 to add more space. I have backup exec. Can anyone tell me the best way to backup SQL so I can be sure it will restore...
2
by: Nate | last post by:
Hello, I am trying to recover a SQL Server 7 database from another hard disk drive that has a corrupted Windows 2000 Advanced Server installation. I am not able to repair the corrupted Windows...
36
by: Ron Johnson | last post by:
http://hardware.devchannel.org/hardwarechannel/03/10/20/1953249.shtml?tid=20&tid=38&tid=49 -- ----------------------------------------------------------------- Ron Johnson, Jr....
9
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a ulimit of 0 bytes. Which calls would be expected to...
12
by: New World Order Pigs | last post by:
Is there no way in .net to get disk space remaining for a given drive??? I can't believe it and yet is seems to be so. If someone knows of a way to do this in the .net libraries I'd be very much...
18
by: Joe Lester | last post by:
This thread was renamed. It used to be: "shared_buffers Question". The old thread kind of died out. I'm hoping to get some more direction by rephrasing the problem, along with some extra...
1
by: Billy Bob | last post by:
hi, whenever i open a compressed file in pen drive, a temporary file forms in windows temporary folder, which disappears as soon as i close compressed file in pen drive. but this temporary file in...
14
by: Lauren Wilson | last post by:
Discovered this interesting comment on MSDN: "To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI)...
4
by: Abhinay | last post by:
Hi, I wanted to check and disply total avilable hard drive space on my machine using C++ on windows. I search on google but did't get any help. Can any budy help me on this issue. Thanks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.