我想在我的 Python 控制台应用程序中获取文件路径作为输入。
目前我只能在控制台中要求完整路径作为输入。
有没有办法触发一个简单的用户界面,用户可以选择文件而不是输入完整路径?
我想在我的 Python 控制台应用程序中获取文件路径作为输入。
目前我只能在控制台中要求完整路径作为输入。
有没有办法触发一个简单的用户界面,用户可以选择文件而不是输入完整路径?
使用 tkinter 怎么样?
from Tkinter import Tk # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
完毕!
Etaoin 的 Python 3.x 版本的完整性回答:
from tkinter.filedialog import askopenfilename
filename = askopenfilename()
使用EasyGui:
import easygui
print(easygui.fileopenbox())
安装:
pip install easygui
演示:
import easygui
easygui.egdemo()
In Python 2 use the tkFileDialog
module.
import tkFileDialog
tkFileDialog.askopenfilename()
In Python 3 use the tkinter.filedialog
module.
import tkinter.filedialog
tkinter.filedialog.askopenfilename()
这对我有用
参考: https ://www.youtube.com/watch?v=H71ts4XxWYU
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)
另一个需要考虑的选择是 Zenity:http ://freecode.com/projects/zenity 。
我有一种情况,我正在开发一个 Python 服务器应用程序(没有 GUI 组件),因此不想引入对任何 Python GUI 工具包的依赖,但我希望我的一些调试脚本由输入文件参数化并想要如果用户未在命令行上指定文件,则直观地提示用户输入文件。Zenity 非常适合。为此,请使用 subprocess 模块调用“zenity --file-selection”并捕获标准输出。当然,这个解决方案不是特定于 Python 的。
Zenity 支持多个平台,并且碰巧已经安装在我们的开发服务器上,因此它有助于我们的调试/开发,而不会引入不必要的依赖项。
我使用 wxPython 获得了比 tkinter 更好的结果,正如对稍后重复问题的回答中所建议的那样:
https://stackoverflow.com/a/9319832
wxPython 版本生成的文件对话框与我在 xfce 桌面上安装的 OpenSUSE Tumbleweed 上的任何其他应用程序中的打开文件对话框看起来相同,而 tkinter 使用不熟悉的横向滚动界面生成了一些狭窄且难以阅读的内容。
这是一个在终端窗口中显示文件选择器的简单函数。此方法支持选择多个文件或目录。即使在不支持 GUI 的环境中也能运行,这具有额外的好处。
from os.path import join,isdir
from pathlib import Path
from enquiries import choose,confirm
def dir_chooser(c_dir=getcwd(),selected_dirs=None,multiple=True) :
'''
This function shows a file chooser to select single or
multiple directories.
'''
selected_dirs = selected_dirs if selected_dirs else set([])
dirs = { item for item in listdir(c_dir) if isdir(join(c_dir, item)) }
dirs = { item for item in dirs if join(c_dir,item) not in selected_dirs and item[0] != "." } # Remove item[0] != "." if you want to show hidde
options = [ "Select This directory" ]
options.extend(dirs)
options.append("⬅")
info = f"You have selected : \n {','.join(selected_dirs)} \n" if len(selected_dirs) > 0 else "\n"
choise = choose(f"{info}You are in {c_dir}", options)
if choise == options[0] :
selected_dirs.add(c_dir)
if multiple and confirm("Do you want to select more folders?") :
return get_folders(Path(c_dir).parent,selected_dirs,multiple)
return selected_dirs
if choise == options[-1] :
return get_folders(Path(c_dir).parent,selected_dirs,multiple)
return get_folders(join(c_dir,choise),selected_dirs,multiple)
要安装查询器,
pip 安装查询
建议root.withdraw()
(也在此处)隐藏窗口而不是删除它,并且在 VS Code 中使用交互式控制台时导致问题(“重复执行”错误)。
下面两个片段返回“打开”或“另存为”(Windows 上的 python 3)中的文件路径:
import tkinter as tk
from tkinter import filedialog
filetypes = (
('Text files', '*.TXT'),
('All files', '*.*'),
)
# open-file dialog
root = tk.Tk()
filename = tk.filedialog.askopenfilename(
title='Select a file...',
filetypes=filetypes,
)
root.destroy()
print(filename)
# save-as dialog
root = tk.Tk()
filename = tk.filedialog.asksaveasfilename(
title='Save as...',
filetypes=filetypes,
defaultextension='.txt'
)
root.destroy()
print(filename)
# filename == 'path/to/myfilename.txt' if you type 'myfilename'
# filename == 'path/to/myfilename.abc' if you type 'myfilename.abc'
我解决了所有相关的问题
from tkinter import * from tkinter import filedialog
只需从pycharm IDE 迁移到Visual Studio Code IDE 即可解决所有问题。