Why am I getting a FileNotFoundError

0 votes

I am working on a program that read a file and searches for a word then prints how many times that word is found in the file. When I  type in "test.rtf" I get this error:

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'

The important part of my code:

fileName= input("Please enter the name of the file you'd like to use.")
fileScan= open(fileName, 'r')  #Opens file

Can someone help me with this?

May 24, 2022 in Python by Kichu
• 19,040 points
531 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The current working directory usually is the directory in which you started the program. In your case, the file test.rtf must be in the same directory in which you execute the program. The best thing you can do is to open the terminal and then cd to the directory where your input file is located. Then use this code:

$ python script.py

This will start the Python script there itself. For this, to work the directory containing the python executable must be in the PATH. 

I hope this helps you.

answered May 28, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Python

0 votes
1 answer

Why am I getting a error when I am printing two different data types in python?

different data type is being used. that ...READ MORE

answered Mar 6, 2019 in Python by Mohammad
• 3,230 points
1,143 views
0 votes
1 answer

Why am I getting the syntax error SyntaxError invalid syntax in a line that contains fully valid syntax?

A "SyntaxError: invalid syntax" is a common ...READ MORE

answered Oct 18, 2023 in Python by anonymous
• 3,360 points
1,087 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,672 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP