在python中使用windows资源管理器浏览器存储文件路径

发布于 2024-09-17 17:10:22 字数 384 浏览 5 评论 0原文

我用 python 编写了一些加密代码,它从用户那里获取原始输入消息,然后使用 AES 对其进行加密和解密。现在我想增强工作,我希望我可以从代码中打开 Windows 资源管理器并浏览到计算机上的任何文件,选择它,当我按“确定”按钮时,文件的路径存储在变量中,以便我可以使用对其进行处理。

我搜索了很多论坛,我已经设法打开Windows资源管理器,但没有传统的“确定”和“取消”按钮。如果用户按“确定”按钮,文件的路径应存储在我的代码变量中。

在这方面的任何帮助将受到高度赞赏。

此外,只是为了让您知道我使用了以下代码:

import os
os.system("start .")

但资源管理器窗口没有任何取消或确定按钮。请帮忙

I have written some encryption code in python that takes raw input message from user and then encrypts and decrypts it using AES. Now i want to enhance the working and i want that i can open the windows explorer from my code and browse to any file on my computer, select it and when i press OK button the path to file is stored in a variable so i can use it for processing.

I have search many forums, i have managed to open windows explorer but there is no traditional OK and Cancel button. If user presses OK button the path to the file should be stored in my code variable.

Any help in this regard will be highly appreciated.

moreover, just to let you know i have used the following code:

import os
os.system("start .")

but the explorer window doesnt have any cancel or OK button. Please help

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

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

发布评论

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

评论(1

少女情怀诗 2024-09-24 17:10:22

这是因为在 Windows 中打开文件时看到的实际上并不是资源管理器窗口,而是称为通用对话框。我假设您指的是此对话框:

打开文件对话框

您可以通过多种方式打开通用打开对话框,其中最简单的可能是使用 Python 标准库中的 Tkinter 模块,即 tkFileDialog 模块的 askopenfilename

示例代码:

import Tkinter
import tkFileDialog

root = Tkinter.Tk()
root.withdraw()

filename = tkFileDialog.askopenfilename(parent=root,title='Open file to encrypt')

至于花括号:您正在使用 askopenfilenames 告诉 Tk 您可能需要多个文件名。这就是为什么您会得到一个用花括号括起来的文件名列表。我实际上怀疑 Python 的 Tk 绑定中存在疏忽,因此文件名不会被分割并返回一个列表,但这可以使用类似于以下的代码轻松修复:

import re
# ...
# ...
filenames = tkFileDialog.askopenfilenames(parent=root)
files_to_process = re.split("\}\W\{", filenames[1:-1])

这将为您提供所选文件名的列表,以防用户选择更多文件名比一个文件。当只传递一个文件名时它会中断,所以一定要检查这种情况。

That is because what you see when you open files in Windows is not actually an Explorer window, it is called a common dialog. I am assuming you are referering to this dialog:

Open file dialog

There are different ways you can go about opening the common open dialog, among the most easiest is probably just using the Tkinter module from the Python standard library, namely the tkFileDialog module's askopenfilename.

Example code:

import Tkinter
import tkFileDialog

root = Tkinter.Tk()
root.withdraw()

filename = tkFileDialog.askopenfilename(parent=root,title='Open file to encrypt')

As for the curly braces: You are using askopenfilenames to tell Tk that you possibly want more than one filename. That's why you get a list of filenames enclosed in curly braces. I actually suspect an oversight in Python's Tk binding so that the filenames are not split up and a list is returned, but this is easily remedied using code similar to this:

import re
# ...
# ...
filenames = tkFileDialog.askopenfilenames(parent=root)
files_to_process = re.split("\}\W\{", filenames[1:-1])

This will give you a list of the selected filenames in case the user selects more than one file. It will break when only passed a single filename, so be sure to check for that case.

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