import tkinter as tk
from tkinter import filedialog
import os
def fileopen():
GUI=tk.Tk()
filepath=filedialog.askopenfilename(parent=GUI,title='Select file to print lines.')
(GUI).destroy()
return (filepath)
filepath = fileopen()
filepath = os.path.normpath(filepath)
with open (filepath, 'r') as fh:
print (fh.read())
或者,使用这种打印线条的方法:
^{pr2}$
或者,如果要将行加载到字符串列表中:
lines = []
fh = open(filepath, 'r')
for line in fh:
line=line.rstrip('\n')
lines.append(line)
fh.close()
for line in lines:
print (line)
如果这会打印文件中的行,那么程序选择的文件可能是空的?我不知道,但试试这个:
或者,使用这种打印线条的方法:
^{pr2}$或者,如果要将行加载到字符串列表中:
当你打开文件时,我想你必须指定你想如何打开它。在您的示例中,您应该打开它进行阅读,例如:
希望这能起作用。在
我解决了。是一个utf-16文件。在
相关问题 更多 >
编程相关推荐