【问题标题】:How do I get the filename of a file to open and the filename of a file to save in python如何获取要打开的文件的文件名和要保存在python中的文件的文件名
【发布时间】:2017-06-01 16:25:58
【问题描述】:

对不起,标题不清楚,我真的不知道该怎么说,但我正在尝试用python制作一个文本编辑器。我想获取用户在 python 中打开或保存的文件的文件名:

def openFile():
    file = filedialog.askopenfile(parent=main_window, mode='rb', title='Select a file')
    contents = file.read()
    textArea.delete('1.0', END)
    textArea.insert('1.0', contents)
    file.close()

在这里,用户会得到一个对话框来选择他想要打开的文件。但是我怎样才能得到他选择的文件的文件名呢?

def saveFileas():
    file = filedialog.asksaveasfile(mode='w')
    data = textArea.get('1.0', END+'-1c')
    file.write(data)
    file.close()

在这里,用户会看到一个对话框来保存他的文件,并输入他们想要的名称。同样,我需要用户输入的名称。两者都使用默认窗口opensave 对话框。

这是我所有的代码:

from tkinter import Tk, scrolledtext, Menu, filedialog, END

main_window = Tk(className = " Text Editor")
textArea = scrolledtext.ScrolledText(main_window, width=100, height=80)

# def newFile():
def openFile():
    file = filedialog.askopenfile(parent=main_window, mode='rb', title='Select a file')
    contents = file.read()
    textArea.delete('1.0', END)
    textArea.insert('1.0', contents)
    file.close()


def saveFileas():
    file = filedialog.asksaveasfile(mode='w')
    data = textArea.get('1.0', END+'-1c')
    file.write(data)
    file.close()

def saveFile():
    content = textArea.get('1.0', END+'-1c')
    if content:
        print("There is content")
    else:
        print("There is no content")


#Menu options
menu = Menu(main_window)
main_window.config(menu=menu)
fileMenu = Menu(menu)
menu.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="New")
fileMenu.add_command(label="Open", command=openFile)
fileMenu.add_command(label="Save As", command=saveFileas)
fileMenu.add_command(label="Save", command=saveFile)
fileMenu.add_separator()
fileMenu.add_command(label="Print")
fileMenu.add_separator()
fileMenu.add_command(label="Exit")

textArea.pack()

main_window.mainloop()

【问题讨论】:

    标签: python filedialog


    【解决方案1】:

    这样的东西应该可以用来获取文件名。

      from  tkinter import filedialog
      root = Tk()
      root.filename =  filedialog.askopenfilename(initialdir = "Path Where the dialog should open first",title = 
      "Title of the dialog",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
      print (root.filename)
      root.withdraw()
    

    【讨论】:

      【解决方案2】:

      documentation 似乎暗示 askopenfile 返回文件名(如果选择了文件名)或空字符串(如果用户单击取消)。换句话说,它是一个字符串,而不是文件指针。您需要先打开文件。

      【讨论】:

        【解决方案3】:

        askopenfile 返回文件对象的句柄,就像 Brian Oakley 所说的那样,您可以像这样获取文件名:

        filePath = filedialog.askopenfile(parent=main_window, mode='rb', title='Select a file')
        fileName = filePath.name.split('/').pop()
        

        【讨论】:

        • askopenfile 返回文件对象的句柄,而不是路径。您的代码将引发错误。
        • 哦,等等,没关系,我使用 askopenfilename 是我的错误。
        • 更新了我的答案以使用 askopenfile 而不是 askopenfilename
        • 你搞错了。 askopenfilename(带有 name)返回一个文件名。 askopenfile(没有 name)返回一个打开的文件句柄。您的代码示例仍然显示askopenfile
        • 是的,知道了,您可以使用askopenfile 返回的文件句柄通过fileHandle.name 获取名称
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多