到目前为止,我遇到的情况如下:
import os.path as op
for d in map(chr, range(98, 123)): #drives b-z
if not op.isdir(d + ':/'): continue
问题是它在 Windows 中弹出“无磁盘”错误框:
maya.exe - 无磁盘:没有磁盘
驱动器。请将磁盘插入
驱动器 \Device\Harddisk1\DR1 [取消、重试、继续]
我无法捕获异常,因为它实际上不会引发 Python 错误。
显然,这种情况仅发生在已分配字母但未插入驱动器的可移动驱动器上。
有没有一种方法可以解决这个问题,而无需专门告诉脚本要跳过哪些驱动器?
在我的场景中,我在学校实验室,驱动器号根据我所在的实验室计算机而变化。另外,我访问磁盘管理的安全权限为零。
Here's what I have so far:
import os.path as op
for d in map(chr, range(98, 123)): #drives b-z
if not op.isdir(d + ':/'): continue
The problem is that it pops up a "No Disk" error box in Windows:
maya.exe - No Disk: There is no disk in
the drive. Please insert a disk into
drive \Device\Harddisk1\DR1 [Cancel, Try Again, Continue]
I can't catch the exception because it doesn't actually throw a Python error.
Apparently, this only happens on removable drives where there is a letter assigned, but no drive inserted.
Is there a way to get around this issue without specifically telling the script which drives to skip?
In my scenario, I'm at the school labs where the drive letters change depending on which lab computer I'm at. Also, I have zero security privileges to access disk management.
发布评论
评论(7)
使用
ctypes
包访问GetLogicalDrives
函数。这不需要 pywin32 等外部库,因此它是可移植的,尽管使用起来有点笨拙。例如:Python 2.7和3.1中添加了itertools.compress;如果您需要支持 <2.7 或 <3.1,这里是该函数的实现:
Use the
ctypes
package to access theGetLogicalDrives
function. This does not require external libraries such as pywin32, so it's portable, although it is a little clunkier to work with. For example:itertools.compress
was added in Python 2.7 and 3.1; if you need to support <2.7 or <3.1, here's an implementation of that function:以下是一种适用于 Windows 和 Linux、Python 2 和 3 的方法:
Here's a way that works both on Windows and Linux, for both Python 2 and 3:
这适用于任何操作系统
this works in any os
如果您有 win32file 模块,则可以调用 GetLogicalDrives():
If you have the win32file module, you can call GetLogicalDrives():
要禁用错误弹出窗口,您需要使用 pywin 设置
SEM_FAILCRITICALERRORS
Windows 错误标志:这告诉 Win32 不要显示重试对话框;当发生错误时,它会立即返回给应用程序。
请注意,这是 Python 调用应该要做的事情。原则上,Python 应该为你设置这个标志。不幸的是,由于 Python 可能嵌入到另一个程序中,因此它无法像这样更改进程范围的标志,并且 Win32 无法以仅影响 Python 而不会影响其余代码的方式指定此标志。
To disable the error popup, you need to set the
SEM_FAILCRITICALERRORS
Windows error flag using pywin:This tells Win32 not to show the retry dialog; when an error happens, it's returned to the application immediately.
Note that this is what Python calls are supposed to do. In principle, Python should be setting this flag for you. Unfortunately, since Python may be embedded in another program, it can't change process-wide flags like that, and Win32 has no way to specify this flag in a way that only affects Python and not the rest of the code.
只要一点点解析是可以接受的,这是一种无需安装 win32api 且无需迭代所有可能的驱动器号的方法。
您还可以解析特定的驱动器类型,例如“网络连接”,通过添加
和 lineSplit[2] == 'Network Connection'
来获取所有网络安装的驱动器号的列表。或者,您可以返回一个字典,而不是返回列表,其中键是驱动器字母,值是 unc 路径 (
lineSplit[3]
)。或者您想从wmic
获取的任何其他信息。要查看更多选项:wmic Logicaldisk get /?
As long as a little parsing is acceptable, this is one way to do it without installing win32api and without iterating through all possible drive letters.
You could also parse for specific drive types, such as "Network Connection" to get a list of all network mounted drive letters by adding
and lineSplit[2] == 'Network Connection'
for example.Alternatively, rather than returning a list, you could return a dictionary, where keys are drive letters and values are unc paths (
lineSplit[3]
). Or whatever other info you want to pull fromwmic
. To see more options:wmic logicaldisk get /?