Python tkFileDialog.askdirectory错误(Python tkFileDialog.askdirectory error)

编程入门 行业动态 更新时间:2024-10-27 16:25:58
Python tkFileDialog.askdirectory错误(Python tkFileDialog.askdirectory error)

我在python中有以下代码

import Tkinter,tkFileDialog top=Tkinter.Tk() from tkFileDialog import askopenfilename dirname = tkFileDialog.askdirectory(parent=top)

当我打印dirname时,它显示为正常,并给出

F:/Project/Dropbox/My Mtech Main Project/Programming Folder/SPIDER/Ubuntu Spyder/

但是,当我访问dirname,否则,即...在命令窗口中的类型dirname我得到以下输出,

u"F:/Project/Dropbox/My Mtech Main Project/Programming Folder/SPIDER/Ubuntu Spyder/"

我怎么能避免这个你,所以我可以使用dirname作为另一个操作的输入,如读取波形文件等。现在我得到了错误,因为你。

i have a following code in python

import Tkinter,tkFileDialog top=Tkinter.Tk() from tkFileDialog import askopenfilename dirname = tkFileDialog.askdirectory(parent=top)

when i print the dirname it appears as normal,and gives

F:/Project/Dropbox/My Mtech Main Project/Programming Folder/SPIDER/Ubuntu Spyder/

But when i acess the dirname,otherwise, ie.. like type dirname in command windows i got the following output,

u"F:/Project/Dropbox/My Mtech Main Project/Programming Folder/SPIDER/Ubuntu Spyder/"

How can i avoid this u ,so that i can use the dirname as an input to another operation like, read a wavefile etc. Now i got the error becuase of that u.

最满意答案

那么path之前的u就是为了表明字符串是unicode type

您可以使用函数type(data)检查type(data) (检查dirname )

Unicode strings are expressed as instances of the unicode type one of Python's repertoire of built-in types

更多信息在这里

如果您想避免这种情况,只需更改类型即可。

import Tkinter,tkFileDialog top=Tkinter.Tk() from tkFileDialog import askopenfilename dirname = str(tkFileDialog.askdirectory(parent=top))

希望有所帮助

Well the u before the path is just to show that the string is of unicode type

you can check the type of data by using the function type(data) (check for dirname)

Unicode strings are expressed as instances of the unicode type one of Python's repertoire of built-in types

more information here

In case you want to avoid this just change the type.

import Tkinter,tkFileDialog top=Tkinter.Tk() from tkFileDialog import askopenfilename dirname = str(tkFileDialog.askdirectory(parent=top))

hope that helps

更多推荐

本文发布于:2023-08-06 15:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1451507.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   tkFileDialog   Python   error   askdirectory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!