创建有道背单词格式的文件

每一行写一个单词或者词组
使用python语言实现、生成的文件就在原目录
代码:
from tkinter import *
import tkinter.filedialog

root = Tk()

def createWordsFile(filenamePath):
    fo = open(filenamePath,"r+")
    print("the opened file:",fo.name)

    print("words begin to create......")
    seq = [];#prepare writes to the file
    for line in fo.readlines():
        line = line.strip()
        line = "#"+line+"#\n"
        seq.append(line)
        
    print("data creating.......")
    print()
    print()

    filename = fo.name
    filename = filename.replace(".txt","词单.txt")
    print("file name:"+filename)

    fo.close()

    print("file creating ......")
    neededFile = open(filename,"w+")
    neededFile.writelines(seq)
    neededFile.close()
    print("**********done***********")




def xz():
    filename = tkinter.filedialog.askopenfilename()
    if filename != '':
        lb.config(text = "您选择的文件是:"+filename);
        createWordsFile(filename)
    else:
        lb.config(text = "您没有选择任何文件");

lb = Label(root,text = '')
lb.pack()
btn = Button(root,text="弹出选择文件对话框",command=xz)
btn.pack()
root.mainloop()

你可能感兴趣的:(创建有道背单词格式的文件)