如何在Windows中检查剩余的硬盘空间? [英] How to check for remaining hard drive space in Windows?

查看:71
本文介绍了如何在Windows中检查剩余的硬盘空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,


我是Python的新手,想知道如何使用Python检查我的Windows机器上剩余的

磁盘空间?我正在考虑使用

命令行dir并尝试从那里提取输出。

但我不知道如何使用Python提取命令行字符串




无论如何,帮助将不胜感激。 :)

解决方案

[...获取可用磁盘空间...]


Sick Monkey写道:


这里是:


>>从win32com.client进口GetObject的
wmiObj = GetObject的(QUOT; winmgmts:\\\\MGW01641\\root\\cimv2")
DISKINFO = wmiObj .ExccQuery(选择*来自Win32_LogicalDisk)
for diskinfo中的磁盘:



... print disk.Name,disk。 FreeSpace

...

答:无

C:16978259968

D:无
< blockquote class =post_quotes>


>>>>



好​​吧,通常没有人打败我的WMI

解决方案:)只是为了不同的是,你也可以看看
查看pywin32扩展的

win32api模块中的GetDiskFreeSpace函数。


doc说:"""

元组= GetDiskFreeSpace(rootPath)


检索有关指定磁盘的信息,包括
$的金额b $ b可用空间。


参数


rootPath:string


指定根磁盘目录返回的信息。如果
rootPath为None,则该方法使用当前目录的根。


Win32 API参考


在msdn,google或google群组中搜索GetDiskFreeSpace。


返回值

返回值是4个整数的元组,包含
每个群集
扇区,每个扇区的字节数,磁盘上的
免费群集总数以及磁盘上的群集总数。

如果函数失败,则返回错误。

"""

TJG


谢谢非常适合帮助的人。我得到了代码Sick Monkey提供了

来在我的电脑上工作。现在,我比以往更加困惑。 :)我认为
认为Python提供的唯一标准模块在这里列出:

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


但似乎还有其他模块可供使用我没有

不得不下载第三方代码。有人能指点我这些其他模块的文件,比如win32com.client吗?谢谢

大家帮忙。 :)


另外,我怎么能得到计算机名?我将在另一台机器上运行此代码

,并且每次将它移植到另一台计算机时都不想更改

代码中的计算机名称。谢谢!


Kevin


2月28日下午4:16,Tim Golden< m ... @ timgolden.me.ukwrote :


[...获得免费磁盘空间...]


Sick Monkey写道:


这里是:


>>来自win32com .client进口GetObject的
wmiObj = GetObject的(QUOT; winmgmts:\\\\MGW01641\\root\\cimv2")
DISKINFO = wmiObj.ExecQuery(QUOT;选择*来自Win32_LogicalDisk")
for diskinfo中的磁盘:



... print disk.Name,disk.FreeSpace

。 ..

A:无

C:16978259968

D:无



嗯,经常不会有人打败我的WMI

解决方案:)只是为了与众不同,你也可以看看
看看GetDiskFreeSpace函数在pywin32扩展的

win32api模块中。


文档说:"""

tuple = GetDiskFreeSpace (rootPath)


检索有关指定磁盘的信息,包括

可用空间量。


参数


rootPath:string


指定要返回信息的磁盘的根目录。如果
rootPath为None,则该方法使用当前目录的根。


Win32 API参考


在msdn,google或google群组中搜索GetDiskFreeSpace。


返回值

返回值是4个整数的元组,包含
每个群集
扇区,每个扇区的字节数,磁盘上的
免费群集总数以及磁盘上的群集总数。

如果功能失败,则返回错误。

"""


TJG



刚试过你的解决方案蒂姆,就像一个魅力。 :)


这很棒,因为我甚至不用担心计算机名称。

关于rootPath参数的问题..我将如何通过

呢?我会把它传递给......


tuple = win32api.GetDiskFreeSpace(r''C:'')

或者只是留空并且函数会自动使用.py文件所在的

rootPath吗?


两者都返回了正确的结果。


Kevin


2月28日下午4:24,kevinliu23 < kevinli ... @ gmail.comwrote:


非常感谢帮助人员。我得到了代码Sick Monkey提供了

来在我的电脑上工作。现在,我比以往更加困惑。 :)我认为
认为Python提供的唯一标准模块在这里列出:

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


但似乎还有其他模块可供使用我没有

不得不下载第三方代码。有人能指点我这些其他模块的文件,比如win32com.client吗?谢谢

大家帮忙。 :)


另外,我怎么能得到计算机名?我将在另一台机器上运行此代码

,并且每次将它移植到另一台计算机时都不想更改

代码中的计算机名称。谢谢!


Kevin


2月28日下午4:16,Tim Golden< m ... @ timgolden.me.ukwrote :


[...获取可用磁盘空间...]


Sick Monkey写道:


这里是:


个肽从win32com.client进口GetObject的

个;>> wmiObj = GetObject的(QUOT; winmgmts:\\\\MGW01641\\\ \\root\\cimv2")

个;>> DISKINFO = wmiObj.ExecQuery(QUOT;从的Win32_LogicalDisk" SELECT *)

个;>> ;对于diskinfo中的磁盘:

... print disk.Name,disk.FreeSpace

...

A:无

C:16978259968

D:无


嗯,这是'不经常有人打败我WMI

解决方案:)只是为了与众不同,你也可以在

中看看
中的GetDiskFreeSpace函数pywin32扩展的win32api模块。


文档说:"""

tuple = GetDiskFreeSpace(rootPath)


检索有关指定磁盘的信息,包括

可用空间量。


参数


rootPath:string


指定要返回的信息的磁盘根目录。如果
rootPath为None,则该方法使用当前目录的根目录。

的Win32 API参考
在MSDN为GetDiskFreeSpace
搜索,谷歌或谷歌组。


返回值

返回值是4个整数的元组,包含
$ b的数量每个群集$ b扇区,每个扇区的字节数,磁盘上的
免费群集总数以及磁盘上的群集总数。

如果功能失败,返回错误。

"""


TJG



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

解决方案

[... 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


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



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



这篇关于如何在Windows中检查剩余的硬盘空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆