我正在尝试制作一个包含用于文件选择的easygui的单词计数器程序。我目前使用带有 PyDev 插件的 Eclipse SDK(如果有更好的 Python 环境的建议)。
这是我当前状态的代码:
#This program is supposed to take a word file, and count the amount of lines and
#words. If the entered file is not a .txt, .doc, or .docx, then the program will
#ask for a different file.
from easygui import fileopenbox
filename = fileopenbox()
lines, words = 0, 0
#This method will count the amount of lines and words in a program and display
#it to the user
def word_count():
if filename.endswith('.docx'): #If the file extension is .docx
print("Your file has" + num_words + "words") #Print the amount of lines and words in the file.
elif filename.endswith('.doc'): #If the file extension is .doc
#<CODE WHICH COUNTS LINES AND WORDS>
print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
elif filename.endswith('.txt'): #If the file extension is .txt
#<CODE WHICH COUNTS LINES AND WORDS>
print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
elif filename.endswith('.py'): #If the file extension is .py
#<CODE WHICH COUNTS LINES AND WORDS>
print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
elif filename.endswith('.java'): #If the file extension is .java
#<CODE WHICH COUNTS LINES AND WORDS>
print("Your file has", lines, "lines, and" ,"words") #Print the amount of lines and words in the file.
else:
print("Are you trying to annoy me? How about giving me a TEXT or SOURCE CODE file, genius?")#Print an insulting error message.
如代码所示,我希望程序读取文件扩展名,如果它与其中任何一个匹配,则运行字数统计代码。但是,我的问题是,字数统计代码是什么?似乎使用 easygui 中的 fileopenbox() 会使事情变得更加复杂。提前感谢任何帮助