在Python中打开文件资源管理器,如果已经打开并且在同一目录中,则打开focus

2024-06-06 13:38:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中,我想打开一个文件资源管理器到一个特定的目录,或者如果它已经在另一个窗口中打开,那么就把焦点放在那个窗口上。目前我正在做这个:

os.startfile(path, 'explore')

但它打开的文件资源管理器与调用os.startfile一样多。在Python中,有没有可能把注意力集中在现有的一个上?在


Tags: 文件path目录osexplore焦点资源管理注意力
1条回答
网友
1楼 · 发布于 2024-06-06 13:38:07

无意中发现了这个问题:在同一个目录下多次打开资源管理器,这与您想要的完全相反。在

来自文档

os.startfile

When operation is not specified or 'open', this acts like double-clicking the file in Windows Explorer, or giving the file name as an argument to the start command from the interactive command shell: the file is opened with whatever application (if any) its extension is associated.

所以就这么做

os.startfile(path)

或者

os.startfile(path, 'open')

只要打开的资源管理器仍在您开始使用的目录中,就可以执行您想要的操作。我不知道我是不是正确的,因为我的答案是假设你在win7操作系统上,使用的是python2.7。在

相关问题 更多 >