我确信之前已经回答了,但我找不到任何可以帮助我...
我正在尝试编写一个简单的程序来读取文件并搜索一个单词,然后打印该文件中找到该单词的次数.好吧,每当我输入"test.rtf"(这是我的文档的名称)时,我都会收到此错误...
Traceback (most recent call last):
File "/Users/AshleyStallings/Documents/School Work/Computer Programming/Side Projects/How many? (Python).py", line 9, in <module>
fileScan= open(fileName, 'r') #Opens file
FileNotFoundError: [Errno 2] No such file or directory: 'test.rtf'
Run Code Online (Sandbox Code Playgroud)
在上学期的课堂上,我似乎记得我的教授说你必须将文件保存在特定的地方?我不确定他是否真的这么说,但是如果有帮助的话,我正在运行苹果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": …Run Code Online (Sandbox Code Playgroud) 我正在尝试添加if语句来检查无效输入.如果用户输入"是"并且如果用户输入"否"则结束,它就像它应该的那样工作并再循环回来.但是出于某些奇怪的原因,无论答案是什么:是,否,随机字符等.它总是打印"无效输入"语句.当答案不是"是"或"否"时,我试图将其打印出来.
while cont == "Yes":
word=input("Please enter the word you would like to scan for. ") #Asks for word
capitalized= word.capitalize()
lowercase= word.lower()
accumulator = 0
print ("\n")
print ("\n") #making it pretty
print ("Searching...")
fileScan= open(fileName, 'r') #Opens file
for line in fileScan.read().split(): #reads a line of the file and stores
line=line.rstrip("\n")
if line == capitalized or line == lowercase:
accumulator += 1
fileScan.close
print ("The word", word, "is in the file", accumulator, "times.")
cont = input ('Type "Yes" …Run Code Online (Sandbox Code Playgroud)