为什么在Python 3中打印字符串时会收到语法错误?
>>> print "hello World"
File "<stdin>", line 1
print "hello World"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud) 我有一个简单的脚本,它解析文件并将其内容加载到数据库.我不需要UI,但是现在我提示用户使用raw_input哪个文件解析最不友好,特别是因为用户无法复制/粘贴路径.我想快速简便地向用户呈现文件选择对话框,他们可以选择文件,然后将其加载到数据库.(在我的用例中,如果他们碰巧选择了错误的文件,它将无法解析,即使它被加载到数据库也不会有问题.)
import tkFileDialog
file_path_string = tkFileDialog.askopenfilename()
Run Code Online (Sandbox Code Playgroud)
这段代码接近我想要的,但它留下一个恼人的空框架打开(无法关闭,可能是因为我没有注册一个关闭事件处理程序).
我不必使用tkInter,但由于它在Python标准库中,因此它是最快和最简单的解决方案的良好候选者.
什么是在没有任何其他UI的情况下在脚本中提示文件或文件名的快速简便方法?
Python 3中的tkFileDialog模块在哪里?使用简单的Dialog在Python中选择文件的问题引用模块使用:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Run Code Online (Sandbox Code Playgroud)
但在Python 3中使用它(在将Tkinter更改为tkinter之后)获得:
Traceback (most recent call last):
File "C:\Documents and Settings\me\My Documents\file.pyw", line 5, in <module>
import tkFileDialog
ImportError: No module named tkFileDialog
Run Code Online (Sandbox Code Playgroud)
python 2.7.2 doc(docs.python.org)说:
tkFileDialog
Common dialogs to allow the user to specify a file to open or save.
These have been renamed as well in Python 3.0; they were all made submodules of the new tkinter package.
Run Code Online (Sandbox Code Playgroud)
但它没有提示新名称是什么,在3.2.2文档中搜索tkFileDialog和askopenfilename根本不返回任何内容(甚至没有从旧名称到新子模块名称的映射.)
尝试显而易见的不做杰克:
from tkinter import askopenfilename, asksaveasfilename
ImportError: …Run Code Online (Sandbox Code Playgroud) 我正在考虑webbrowser模块的内容,但是对于文件浏览器.在Windows中我想打开资源管理器,在Linux上的GNOME中我想在KDE上打开nautilus,Konqueror等等.如果我可以避免它,我宁愿不要破解它.;-)
我正在学习Python中的基本GUI,我遇到了一个示例,用于从Stack Overflow上的文件资源管理器中读取文件名.
from Tkinter import Tk
from tkFileDialog 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)
Run Code Online (Sandbox Code Playgroud)
当我尝试在IDLE中运行它时,这个特殊的脚本工作正常,但是如果我在Windows 7中从命令提示符处尝试,则不会运行.
Python版本:2.7.这是我得到的输出错误.
>>> from Tkinter import Tk
>>> from tkFileDialog import askopenfilename
>>> Tk().withdraw()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1685, in __init__
self.tk = _tkinter.create(screenName, …Run Code Online (Sandbox Code Playgroud) 我做自动化测试并获得文件对话框.我想从带有python或selenium的windows打开文件对话框中选择一个文件.
注意:该对话框由其他程序提供.我不想用Tkinter创建它.
窗口看起来像:
.
这该怎么做?