python找不到文件数据_在Python中找不到文件错误

我相信这是以前的答案,但我找不到任何帮助我。。。

我正在尝试编写一个简单的程序来读取一个文件并搜索一个单词,然后打印该单词在文件中出现的次数。好吧,每次我输入“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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值