我相信这是以前的答案,但我找不到任何帮助我。。。
我正在尝试编写一个简单的程序来读取一个文件并搜索一个单词,然后打印该单词在文件中出现的次数。好吧,每次我输入“test.rtf”(这是我文档的名称)时,我都会得到这个错误。。。Traceback (most recent call last):
File "/Users/AshleyStallings/Documents/School Work/Computer Programming/Side Projects/How many? (Python).py", line 9, in
fileScan= open(fileName, 'r') #Opens file
FileNotFoundError: [Errno 2] No such file or directory: 'test.rtf'
在上学期的课上,我好像记得我的教授说你必须把文件保存在一个特定的地方?我不确定他是否真的这么说,但如果这有帮助的话,我正在运行苹果OSx。哈哈
这是我的代码,任何帮助都是感激的:)提前谢谢!print ("Hello! Welcome to the 'How many' program.")
fileName= input("Please enter the name of the file you'd like to use. Make \
sure to include the correct extension!") #Gets file name
fileScan= open(fileName, 'r') #Opens file
cont = "Yes"
accumulator = 0
while cont == "Yes":
word=input("Please enter the word you would like to scan for.") #Asks for word
capitalized= word.capitalize()
lowercase= word.lower()
print ("\n")
print ("\n") #making it pretty
print ("Searching...")
for word in fileScan.read(): #checking for word
accumulator += 1
print ("The word ", word, "is in the file ", accumlator, "times.")
cont = input ('Type "Yes" to check for another word or \
"No" to quit.') #deciding next step
cont = cont.capitalize()
if cont != "No" or cont != "Yes": #checking for valid input
print ("Invalid input.")
cont = input ('Type "Yes" to check for another word or \
"No" to quit.')
cont = cont.capitalize()
print ("Thanks for using How many!") #ending