我确信之前已经回答了,但我找不到任何可以帮助我…
我正在尝试编写一个简单的程序来读取文件并搜索一个单词,然后打印该文件中找到该单词的次数.好吧,每当我输入“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