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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值