简体   繁体   English

在Python中,如何在Windows中将本地硬盘与网络和软盘分开?

[英]in Python, how to separate Local Hard Drives from Network and Floppy in Windows?

I've been looking for this info for awhile, and I have a number of ways to retrieve a list of local drives under Windows. 我一直在寻找这个信息,我有很多方法可以在Windows下检索本地驱动器列表。 Here are two examples: 这是两个例子:

print win32api.GetLogicalDriveStrings().split("\x00")

and

def getDriveLetters(self):
    self.drvs = []
    n_drives = win32api.GetLogicalDrives()
    for i in range(0,25): #check all drive letters
        j = 2**i # bitmask for each letter
        if n_drives & j > 0:
            self.drvs.append(chr(65+i)+":/")
    print self.drvs

What I can't seem to find is a way to separate the floppies (A:), usb drives (G:), CD drives (E:), and network drives (P:) from the Local hard drives (C:, D:) 我似乎无法找到一种方法来从本地硬盘驱动器(C:,分离)软盘(A :),USB驱动器(G :),CD驱动器(E :)和网络驱动器(P :) d :)

If they all were assigned the same letters it would be easy, but I'm writing this script to monitor local hard disk space across a network of computers with different configurations. 如果它们都被分配了相同的字母,那将很容易,但我正在编写此脚本来监视具有不同配置的计算机网络中的本地硬盘空间。

Any help would be appreciated! 任何帮助,将不胜感激! Thanks. 谢谢。

You can try the win32 GetDriveType function. 您可以尝试win32 GetDriveType函数。

import win32file
>>> win32file.GetDriveType("C:/") == win32file.DRIVE_FIXED ##hardrive
True
>>> win32file.GetDriveType("Z:/") == win32file.DRIVE_FIXED ##network
False
>>> win32file.GetDriveType("D:/") == win32file.DRIVE_FIXED ##cd-rom
False

Thank you for your post - helped me with a ruby port. 谢谢您的帖子 - 帮我一个红宝石端口。 Method getDriveLetters returns hash (dict): drive letter string, drive type string. 方法getDriveLetters返回hash(dict):驱动器盘符串,驱动器类型字符串。

require 'Win32API'

GetLogicalDrives = Win32API.new('kernel32', 'GetLogicalDrives', 'V', 'L')
GetDriveType = Win32API.new('kernel32', 'GetDriveType', 'P', 'I')

def GetDriveType(path)
    GetDriveType.call(path)
end

def GetLogicalDrives()
    GetLogicalDrives.call()
end

def getDriveLetters
    drivetype = {
        0 => 'DRIVE_UNKNOWN',
        1 => 'DRIVE_NO_ROOT_DIR',
        2 => 'DRIVE_REMOVABLE',
        3 => 'DRIVE_FIXED', 
        4 => 'DRIVE_REMOTE', 
        5 => 'DRIVE_CDROM', 
        6 => 'DRIVE_RAMDISK'
    }

    drvs = []
    n_drives = GetLogicalDrives()

    for i in 0..25 do #check all drive letters
        j = 2**i # bitmask for each letter
        if n_drives & j > 0 then
            drive = (65+i).chr + ":/"
            drvs += [drive => drivetype[GetDriveType(drive)]]
        end
    end
    return drvs
end

puts getDriveLetters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python tkinter Combobox不会填充Windows硬盘驱动器 - python tkinter Combobox doesn't populate windows hard drives Python搜索多个硬盘 - Python search multiple hard drives 如何在Python / Windows中获取没有替换的本地驱动器列表? - How to get a list of local drives without SUBST'ituted ones in Python / Windows? 使用硬盘上的文件而不是python的url - Use of files on hard drives instead of url with python 如何使用Python检测Ubuntu / Linux上仅USB和硬盘插入? - How can I detect using Python the insertion of only USBs and hard drives on Ubuntu/Linux? 为什么 Python 的 pathlib 和 os lib 为 Windows 中的映射网络驱动器返回不同的结果? - Why does Python's pathlib and os lib return different results for mapped network drives in Windows? 如何在Windows中获取物理驱动器 - how to obtain physical drives in Windows 可以连接到本地网络上的套接字,但不能连接到单独的套接字(python 套接字) - Can connect to a socket on my local network but not on separate ones (python sockets) 如何使用 python 将图像文件从本地硬盘加载到 html 页面? - How to load an image file from my local hard drive to html page using python? 从Windows本地网络连接到Firebird数据库 - Connecting to Firebird database from Windows local network
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM