Jump to content

Hello everyone! I have created a program that is supposed to read files. All the codes are correct, but I'm just getting an error that it can't find a file. I create a simple txt file using notepad and saving it. I am still getting errors it cant find it but I know its there. Can someone help me? Thanks!

 

fname = input("Enter the name of the file:")
infile = open(fname, "r")
lines = 0
words = 0
characters = 0
for line in infile:
    wordslist = line.split()
    lines = lines + 1
    words = words + len(wordslist)
    characters = characters + len(line)
print(lines)
print(words)
print(characters)

THIS IS THE ERROR IM GETTING WHEN RUNNING IT BELOW:

Enter the name of the file:lamb
Traceback (most recent call last):
  File "C:\Users\Bryan\AppData\Local\Programs\Python\Python38\HW extra credit #1.py", line 2, in <module>
    infile = open(fname, "r")
FileNotFoundError: [Errno 2] No such file or directory: 'lamb'
>>> 
 RESTART: C:\Users\Bryan\AppData\Local\Programs\Python\Python38\HW extra credit #1.py 
Enter the name of the file:lamb.txt
Traceback (most recent call last):
  File "C:\Users\Bryan\AppData\Local\Programs\Python\Python38\HW extra credit #1.py", line 2, in <module>
    infile = open(fname, "r")
FileNotFoundError: [Errno 2] No such file or directory: 'lamb.txt'

 

Link to comment
https://linustechtips.com/topic/1062290-python-help3/
Share on other sites

Link to post
Share on other sites

If the file in the same place as your python script? 

 

In python it will take context from where you run it.

 

So if you have your script in a folder called fileReader when you run python in the folder it will look for

 

Path/to/fileReader/lamb.txt

 

If the file was in a folder called files you would need to load files/lamb.txt

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/1062290-python-help3/#findComment-12552111
Share on other sites

Link to post
Share on other sites

11 hours ago, fpo said:

When you type in it’s best to use a complete directory. IE

 


C:/users/JohnDoe/documents/file and.txt

 

you need quotes if you have ANY spaces. 

This isn't required for things like custom modules so why would it be needed here? 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/1062290-python-help3/#findComment-12554737
Share on other sites

Link to post
Share on other sites

15 minutes ago, fpo said:

It’s required for c# so I thought it would be required for python too. 

Even c# should be able to take a relative path else how would programs work across machines?

 

So we both pull the same code I won't have the same file path as you.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/1062290-python-help3/#findComment-12555954
Share on other sites

Link to post
Share on other sites

1 hour ago, vorticalbox said:

Even c# should be able to take a relative path else how would programs work across machines?

 

So we both pull the same code I won't have the same file path as you.

C# can do relative file paths. 

I prefer absolute paths. 

I just phrased things poorly. 

Link to comment
https://linustechtips.com/topic/1062290-python-help3/#findComment-12556180
Share on other sites

Link to post
Share on other sites

There can be only 2 mistakes.

One was pointed out, file isnt in same folder as script.

but.... 

probably its this problem: 

You didnt typed extension in the input. That means you cant input just filename you need to input filename.txt (or whatever extension it has)

 

instead of lamb, try with lamb.txt

 

or 

change code to infile = open(fname = '.txt', "r")

Link to comment
https://linustechtips.com/topic/1062290-python-help3/#findComment-12564845
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×