【问题标题】:Is there a way to get the name of the file only from tkFileDialog?有没有办法只从 tkFileDialog 获取文件名?
【发布时间】:2011-10-20 04:08:11
【问题描述】:

我有这个代码:

filename = tkFileDialog.askopenfilename(initialdir="lists/custom/", filetypes=(("Word list",
                                        "*.tldr"), ("All files", "*.*")))

如果我有一个名为“dog.tldr”的文件,有没有办法只获取它的文件名而不是在它前面有一个目录?现在filename 输出类似

/dir/blah/dog.tldr

但我只想要dog.tldr。有什么办法吗?

谢谢。

【问题讨论】:

    标签: python file tkinter


    【解决方案1】:

    os.path.split:

    In [83]: full_path = "/dir/blah/dog.tldr"
    
    In [84]: os.path.split(full_path)
    Out[84]: ('/dir/blah', 'dog.tldr')
    
    In [85]: os.path.split(full_path)[1]
    Out[85]: 'dog.tldr'
    

    【讨论】:

    • os.path.basename() 会更好。
    猜你喜欢
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    相关资源
    最近更新 更多