|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- def txt_create(name, msg):
- path = 'F:\\'
- fullpath = path + name + '.txt'
- f = open(fullpath, 'w')
- f.write(msg)
- f.close()
- print('Over!')
- txt_create('demo', 'Hi, boy!')
复制代码
上面是我自定义的一段创建txt文件的函数,它实现了创建和写入的功能。但在写函数执行体时,还是调用了Python中的open函数和write函数。请问,我现在不想调用open创建文件和write写入文件,我想自己定义函数,一个用来创建txt文件,一个用来读取txt文件,一个用来写入txt文件,烦请大佬指点一下,我该如何写这三个函数。
我好像有一个办法,Tkinter!
- from tkinter import *
- import tkinter.filedialog
- #创建主窗口
- win = Tk()
- win.title(string = "打开文件和保存文件")
- #打开一个[打开旧文件]对话框
- def createOpenFileDialog():
- myDialog1.show()
- #打开一个[另存新文件]对话框
- def createSaveAsDialog():
- myDialog2.show()
- #按下按钮后,即打开对话框
- Button(win, text="打开文件", command=createOpenFileDialog).pack(side=LEFT)
- Button(win, text="保存文件",command=createSaveAsDialog).pack(side=LEFT)
- #设置对话框打开的文件类型
- myFileTypes = [('Python files', '*.py *.pyw'), ('All files', '*')]
- #创建一个[打开旧文件]对话框
- myDialog1 = tkinter.filedialog.Open(win, filetypes=myFileTypes)
- myDialog2 = tkinter.filedialog.SaveAs(win, filetypes=myFileTypes)
- win.mainloop()
复制代码
|
|