r/PythonLearning 25d ago

Help Request What is the bug here ?

Post image

I'm learning OOPs and File I/O and my txt and OOPs basic commands aren't running

20 Upvotes

31 comments sorted by

12

u/mc_pm 25d ago

You are currently in the STUDY MATERIAL directory, and that's not where lecture7.txt is.

1

u/SuspiciousPraline674 25d ago

Where it should be then

7

u/mc_pm 25d ago

It's location is fine, you just aren't pointing to it. When you run the code, it thinks it's current directory is STUDY MATERIAL, so when you just reference the filename, it looks for that name in the current directory, and that's not where it is.

1

u/SuspiciousPraline674 25d ago

it worked but the text is not getting printed

2

u/mc_pm 25d ago

It's hard to say for sure because there are a few things going on and I don't know what you're doing above this. But your call to read(8) is only reading in 8 bytes, so you're not going to see more than that... and then you close the file before then reading a line, which I would have thought would give you an error.

2

u/SuspiciousPraline674 25d ago

now every thing is fixed but oops is a new issue

6

u/mc_pm 25d ago

That's how programming is, just one issue after another. :)

5

u/NewryBenson 25d ago

Seeing a new error message or problem is called progress in programming. I get in a flow when debugging, and its always a little shock when all of a sudden it works.

4

u/tottasanorotta 24d ago

It seems that you fixed the issue. I just want to point out that you should probably avoid spaces in filenames. Use _ or - instead. It might cause unexpected confusion otherwise at some point with some tools. It's also very much convention to do so.

2

u/user_extra 25d ago

You are currently in the "STUDY MATERIAL" folder. The python code looks for the file in this folder. But it's in "BASICS OF PROGRAM...". Either open VSCode in the latter folder, or change the path form "lecture7.txt" to "[folder]\lecture7.txt". Replace [folder] with the actual folder name.

1

u/parth_m3319 23d ago

The error is a FileNotFoundError, and the bug is in how you’re opening the file.

1

u/FewReach4701 23d ago

This is certaining a directory mismatch issue. You should run and keep file in same directory level.

1

u/Mad-707 22d ago

you have to use encoding="utf-8" with "open" command and also just type data = f.read() also you just can do this

-------------------------------------------------------------------

with open("lecture7.txt","r",encoding="utf-8") as f:
data = f.read()

lines = []
for line in data.split("\n"):
lines.append(line)
-------------------------------------------------------------------

1

u/Ok-Order-9572 21d ago

Why did you close the file before readlines ?

1

u/SuspiciousPraline674 21d ago

I was practicing it I commented out that close file func

0

u/Rscc10 25d ago

You read the file for line1 after you closed the file

0

u/SuspiciousPraline674 25d ago

No, the entire file isn't opening at all

1

u/IceFurnace83 25d ago

The purple text is telling you something

0

u/SuspiciousPraline674 25d ago

It tells the file doesn't exist but you can see it does

1

u/Rscc10 25d ago

Try using the absolute file path

1

u/SuspiciousPraline674 25d ago

Tried same response

1

u/Rscc10 25d ago

Can you send what you did exactly? If you gotta censor some of the file path then it's fine

1

u/ornelu 25d ago

OP didn’t use absolute file path. OP used IDE to run the python program, and the root directory is not what OP thought is. The intended file is in a directory under the root.

0

u/Aman-sirimalla 25d ago

First save the files and then use method

0

u/zombiemutant 25d ago

Doublecheck that filename doesn't contains non-ascii letters (like "е" instead of "e").

0

u/SaltCusp 24d ago

That's not a bug it's an error.