这是我的任务。 给我这样的模板... def_choosefile():
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
return filedialog.askopenfilename()
因此,如果我正确无误,它将提示一个对话框,要求选择一个文件。并且当选择了文件时,程序应该告诉它选择了哪些文件。使用这些:
selected_file = choose_file()
print ("Selected file: {}".format (selected_file))
之后,如何使程序正常读取文件?通常我会使用:
infile = open('text')
infile.readline()
答案 0 :(得分:0)
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
def choose_file():
return filedialog.askopenfilename()
selected_file = choose_file()
with open (selected_file,'r') as readfile:
lines = readfile.read().splitlines()
for line in lines[0:2]:
print (line)