473,602 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22607
[... re getting free disk space ...]

Sick Monkey wrote:
Here you are:
>>from win32com.client import GetObject
wmiObj = GetObject("winm gmts:\\\\MGW016 41\\root\\cimv2 ")
diskinfo = wmiObj.ExecQuer y("Select * from Win32_LogicalDi sk")
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 GetDiskFreeSpac e function in the
win32api module of the pywin32 extensions.

The doc says: """
tuple = GetDiskFreeSpac e(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 GetDiskFreeSpac e 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("winm gmts:\\\\MGW016 41\\root\\cimv2 ")
diskinfo = wmiObj.ExecQuer y("Select * from Win32_LogicalDi sk")
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 GetDiskFreeSpac e function in the
win32api module of the pywin32 extensions.

The doc says: """
tuple = GetDiskFreeSpac e(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 GetDiskFreeSpac e 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.GetDis kFreeSpace(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...@gma il.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("winm gmts:\\\\MGW016 41\\root\\cimv2 ")
>>>diskinfo = wmiObj.ExecQuer y("Select * from Win32_LogicalDi sk")
>>>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 GetDiskFreeSpac e function in the
win32api module of the pywin32 extensions.
The doc says: """
tuple = GetDiskFreeSpac e(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 GetDiskFreeSpac e 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_ComputerS ystem 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 GetDiskFreeSpac e function in the
win32api module of the pywin32 extensions.
The MSDN page for that function warns:
"The GetDiskFreeSpac e 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 GetDiskFreeSpac eEx 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 GetDiskFreeSpac e. 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 GetDiskFreeSpac eEx. I will
look into this function as well.

Thanks guys!

On Feb 28, 5:08 pm, "Jerry Hill" <malaclyp...@gm ail.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 GetDiskFreeSpac e function in the
win32api module of the pywin32 extensions.

The MSDN page for that function warns:
"The GetDiskFreeSpac e 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 GetDiskFreeSpac eEx 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.GetDis kFreeSpace(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_Logical Disk (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********@gma il.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.GetDis kFreeSpace(r'C: ')
or just leave it blank and the function will automatically use the
rootPath of where the .py file resides?
For GetDiskFreeSpac e, the argument *must* end in \, so you should use
GetDiskFreeSpac e('C:\\')
Using GetDiskFreeSpac eEx, 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********@gmai l.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

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

Similar topics

3
2476
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 properly? I was thinking of using imaging software and creating an image to a USB 2.0 hard drive. The DB is 80 gigs. Thanks NH
2
2942
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 2000 Advanced Server installation but the file system is intact. I have installed a new copy of SQL Server 7 onto a new hard disk and have used the sp_attach_db system stored procedure to attach the database from the old hard drive into the new...
36
3946
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. ron.l.johnson@cox.net Jefferson, LA USA I can't make you have an abortion, but you can *make* me pay child support for 18 years? However, if I want the child (and all the expenses that entails) for the *rest*of*my*life*, and you
9
2869
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 return error codes for such conditions? Would it happen on the writes (fwrite, fprintf, fputs ...), or as they actually write to io buffers, might the errors not occur until the data is flushed or the file is closed?
12
10995
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 appreciative. Thanks, LT.
18
5117
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 observations I've recently made. The core of the problem is that Postgres is filling up my hard drive with swap files at the rate of around 3 to 7 GB per week (that's Gigabytes not Megabytes) . At this rate it takes roughly two months to fill up my 40...
1
1386
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 windows temporary folder which has though disappeared, can be recovered by using recovery softwares. so when one works on one's pen drive on some other person's pc, one is likely to leave one's personal data on that computer's hard drive....
14
28111
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) Win32_PhysicalMedia (a class) property SerialNumber." I'm sorry to admit it bit I am really undereducated on how to incorporate some of the Windows SDK stuff into VBA apps. Anyone know of some sample code that will allow us to read the C drive hardware...
4
6156
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 in advance. Abhinay
0
7993
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.