从 Python 中查找系统硬盘驱动器?

发布于 2024-09-28 18:21:00 字数 104 浏览 2 评论 0原文

我正在为我当前的应用程序开发一个软件安装程序。需要安装到系统硬盘上。如何检测系统驱动器并从Python返回盘符?

win32 扩展有用吗? Python预打包的os模块怎么样?

I am working on a software installer for my current application. It needs to be installed to the System HDD. How owuld I detect the system drive and return the letter from Python?

Would the win32 extensions be useful? How about the os module pre packaged with Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪荡不羁 2024-10-05 18:21:00

这是在 Win32 平台上返回系统驱动器号的方法:

import os
print os.getenv("SystemDrive")

上面的代码片段返回系统驱动器号。就我而言(以及 Windows 上的大多数情况)C:

This is how to return the letter of the System drive on a Win32 platform:

import os
print os.getenv("SystemDrive")

The above snippet returns the system drive letter. In my case ( and most cases on windows) C:

鯉魚旗 2024-10-05 18:21:00

如果您安装了 win32 扩展,则以下内容将为您提供所需的信息:

In [82]: import win32api

In [83]: drives = win32api.GetLogicalDriveStrings()

In [84]: drives
Out[84]: 'C:\\\x00D:\\\x00E:\\\x00'

In [85]: drives.split('\x00')
Out[85]: ['C:\\', 'D:\\', 'E:\\', '']

忽略最后一项,因为 win32 的 GetLogicalDriveStrings 函数。

If you install the win32 extensions, the following will get you the information you want:

In [82]: import win32api

In [83]: drives = win32api.GetLogicalDriveStrings()

In [84]: drives
Out[84]: 'C:\\\x00D:\\\x00E:\\\x00'

In [85]: drives.split('\x00')
Out[85]: ['C:\\', 'D:\\', 'E:\\', '']

Ignore the last item, due to a terminating character in the string returned by win32's GetLogicalDriveStrings function.

所有深爱都是秘密 2024-10-05 18:21:00
import win32api #https://timgolden.me.uk/pywin32-docs/win32api.html
print("GetWindowsDirectory:", win32api.GetWindowsDirectory()[0])
#or
print("GetSystemDirectory:", win32api.GetSystemDirectory())

输出:

获取Windows目录:C

获取系统目录:C:\Windows\system32

import win32api #https://timgolden.me.uk/pywin32-docs/win32api.html
print("GetWindowsDirectory:", win32api.GetWindowsDirectory()[0])
#or
print("GetSystemDirectory:", win32api.GetSystemDirectory())

Output:

GetWindowsDirectory: C

GetSystemDirectory: C:\Windows\system32

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文