简体   繁体   English

打开和读取带有askopenfilename的文件

[英]Opening and reading a file with askopenfilename

I have the following code where I'm trying to allow the user to open a text file and once the user has selected it, I would like the code to read it (this isn't a finished block of code, just to show what I'm after). 我有以下代码试图允许用户打开文本文件,一旦用户选择了它,我希望代码读取它(这不是完整的代码块,只是为了显示什么内容)我在追)。

However, I'm having difficulties either using tkFileDialog.askopenfilename and adding 'mode='rb'' or using the code like below and using read where it produces an error. 但是,我在使用tkFileDialog.askopenfilename并添加'mode ='rb'或使用下面的代码以及在产生错误的地方使用read时遇到困难。

Does anyone know how I can arrange to do this as I don't wish to have to type Tkinter.'module' for each item such as Menu and Listbox. 有谁知道我该怎么做,因为我不想为菜单和列表框等每个项目都键入Tkinter.'module'。 Beginner to Tkinter and a bit confused! 初学者对Tkinter有点困惑! Thanks for the help! 谢谢您的帮助!

import sys
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename # Open dialog box

fen1 = Tk()                              # Create window
fen1.title("Optimisation")               #

menu1 = Menu(fen1)

def open():

    filename = askopenfilename(filetypes=[("Text files","*.txt")])
    txt = filename.read()
    print txt
    filename.close()

fen1.mainloop()

Obviously the error I'm getting here is: 显然我到达这里的错误是:

AttributeError: 'unicode' object has no attribute 'read'

I don't understand how to use the askopen and also be able to read the file I'm opening. 我不知道如何使用askopen,也无法读取我正在打开的文件。

askopenfilename仅返回文件名,您想要的是askopenfile ,它接受mode参数并为您打开文件。

The filename in your sample code is just that -- a string indicating the name of the file you wish to open. 示例代码中的filename就是这样-一个字符串,指示您要打开的文件的名称。 You need to pass that to the open() method to return a file handle for the name. 您需要将其传递给open()方法以返回名称的文件句柄。 You can then read from the file handle. 然后,您可以从文件句柄读取。

Here's some quick and dirty code to run in the Python interpreter directly. 这是一些可以直接在Python解释器中运行的快速而肮脏的代码。 (You can run this in a script, too, but I really like REPL interfaces for quickly trying things out. You may like it as well.) (您也可以在脚本中运行它,但是我真的很喜欢REPL接口来快速尝试。您也可能喜欢它。)

$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> from tkFileDialog import askopenfilename
>>> root = Tkinter.Tk() ; root.withdraw()
''
>>> filename = askopenfilename(parent=root)
>>> filename
'/tmp/null.c'
>>> f=open(filename)
>>> f.read()
'#include<stdio.h>\n\nint main()\n{\n  for(;NULL;)\n    printf("STACK");\n\n  return 0;\n}\n\n'
>>> f.close()
>>> 

Note especially that there's nothing Tkinter-specific in reading the file -- the dialog box just gives you a filename. 特别要注意的是,在读取文件时没有Tkinter特有的东西-该对话框仅提供文件名。

Your error is the name of your function. 您的错误是函数的名称。 I simply changed def open() for def open1() and it works. 我只是简单地将def open()更改为def open1()

def open1():

    filename = askopenfilename(parent=fen1)
    print(filename)
    f = open(filename)
    txt = f.read()
    print txt
    f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM