Why my .py code can't find any .txt files?

Hello, I am having a problem with my fhand=open() codes I don’t really know why but the code itself is not working, it can’t detect the .txt file even if they are in the same folder. what should I do here is my code and the error it gives to me.
The code:

ffile=input('ENTER THE FILE''S NAME...:')
for line in fhand:
    if not line.starswith('A'):
    c=input('Dosyanda :', sayiLine,'kadar satır bulunuyor.')
    d=input('Dosyanda :', uzunluk,'kadar karakter mevcut, VE BOŞLUKLAR BUNA DAHİL!!')
    f=input('Son olarak dosyanda aramış olduğun tüm satırlar şunlar:')
    g=input('Çıkmak için enter tuşuna basabilirsin')

The error:

Traceback (most recent call last):
  File "c:\Users\PC\Desktop\PYTHON FALL\deneme.py", line 4, in <module>
  File "C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\encodings\cp1254.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9e in position 97: character maps to <undefined>

First if you want to read lines of a text file named “file.txt” why are you even asking the user to input the file’s name?

Anyway, assuming you want to read the file.txt line by line, you can use the readlines method on fhand (the file opened in your code) and then use the for line in lines you have written.

You will quickly run into other errors because there is no such Python method named starswith and the input function only expects one argument to be passed.

input('Dosyanda :', sayiLine,'kadar satır bulunuyor.')

You are attempting to pass 3 arguments (separated by commas) here. If you want to concatenate ‘Dosyanda :’ with sayiLine and then kadar satır bulunuyor., you need to use the + operator.

Thanks for the advice and for showing my mistakes but even this code can’t do its job:

for line in fhand:
print('Line count:', count)

I have the file.txt in the same folder with the code.
Is it about my PC or should I just re-install the visual studio or python?

What error do you get when you run the code above?

Traceback (most recent call last):
  File "c:\Users\PC\Desktop\PYTHON FALL\.vscode\6-deneme.py", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'

It says no such file in directory but I have the file.
It did the same thing two days ago but after trying it 4 times it didn’t give me any errors. Now it gives it again.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.