【问题标题】:How to have windows automatically add an extension to a file?如何让windows自动为文件添加扩展名?
【发布时间】:2013-02-01 09:32:30
【问题描述】:

背景

我目前正在制作一个基本的文本编辑器,希望对 Tkinter 有一个基本的了解。我想制作自己的文件格式,称为.mydoc 我试图将filetype 更改为.mydoc 以不占优势。这是我目前拥有的代码:

代码

def openMe(self):
    #import the Tk file dialogue
    import tkFileDialog as tkF
    myFormat = [('Example Format', '*.mydoc')]
    direct = tkF.askopenfilename(initialdir='D:\\', filetypes = myFormat, title = "Open a .mydoc")
    try:
        #open the text file
        txt_file = open(direct,"r")
    except UnboundLocalError, IOError:
        print "You either did not select a file, or the filetype was incorrect.\nPlease try again."
    #Read the data
    currentTEXT = txt_file.read()
    #Delete current text
    self.write.delete(0.0, END)
    #insert new text
    self.write.insert(0.0, currentTEXT)

问题

  1. 如何让计算机自动添加我的分机? (是的,我已经关闭了hide extensions 选项。

技术规格

语言:Python 2.7.3

操作系统:Windows 7

【问题讨论】:

  • 具体的错误信息或症状是什么?
  • @CaptainMurphy 问题是,没有错误消息。我保存了文件,当我进入 Windows 资源管理器时,它没有扩展名。

标签: python windows python-2.7 tkinter


【解决方案1】:

尝试使用defaultextension

tkF.askopenfilename(initialdir='D:\\',
filetypes=myFormat,
title="Open a .mydoc",
defaultextension=".mydoc")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    相关资源
    最近更新 更多