# prompt: Count the number of lines in a .txt file
def count_lines_in_file(file_path):
"""Counts the number of lines in a text file.
Args:
file_path: The path to the text file.
Returns:
The number of lines in the file, or -1 if the file cannot be opened.
"""
try:
with open(file_path, 'r') as file:
line_count = 0
for line in file:
line_count += 1
return line_count
except FileNotFoundError:
print(f"Error: File not found at '{file_path}'")
return -1
# Replace 'your_file.txt' with the actual path to your file
file_path = 'million_holdouts.txt'
line_count = count_lines_in_file(file_path)
if line_count != -1:
print(f"The file '{file_path}' has {line_count} lines.")
#π Why does this code to read the lines in a file not work?
61 messages Β· Page 1 of 1 (latest)
@plain mantle
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
disclaimer: this was generated by the AI in google colab, and for some reason it's giving me a different line value every time I run it
I just put in my text file (million_holdouts.txt) as the file path and then ran the code
this should work Β―_(γ)_/Β―
it actually does work but I have to run the code a bunch of times
there's 1458704 lines in the file and each time I run it it adds 200k lines until it ends up on the true value
You would want to do for line in file.readlines()
its the same
If you just do for line in file it'll read every character
nope
I'm pretty sure
@plain mantle can you try this and see
pretty sure it is actually reading the lines, but it's prematurely stopping
Hm
Ig you could just do return len(file) to count the lines probably?
they both do the same thing, and I'm getting the same issue for both
that doesnt work, confusingly enough
Damn
because of this
oh I see
the file object, as far as i know, is basically a pointer to some byte. when you iterate over it, it moves to the next newline character & loads everything in between, but now the pointer points to the next line, so that first line becomes sort of unreachable
I mean, this works
line_count = len(file.readlines())
yeah that should
i am curious about the inconsistency though, i dont know how you could be getting different outputs with the same input without some strange shenanigans going on
it's weird
The file 'million_holdouts.txt' has 174763 lines.
The file 'million_holdouts.txt' has 823882 lines.
The file 'million_holdouts.txt' has 1123475 lines.
The file 'million_holdouts.txt' has 1458704 lines.
The file 'million_holdouts.txt' has 1458704 lines.
The file 'million_holdouts.txt' has 1458704 lines.
this is the output every time I run it
1458704 is the correct value
I get the same issue even with this
and these are separate runtimes? not multiple function calls, but the program actually exits, and you run it again, and still get this output?
they should be, I'm rerunning the entire code block
it's really weird
that is so interesting
its not even like they are incrementing by some power of 2
at least then i could chalk it up to something to do with c maybe not having big enough numbers for the pointer position
something along those lines
the incrementing value is different each time I do it
(I have to reset and delete the runtime and reupload the file for it to not work again)
What if you do
for line, val in enumerate(file.readlines()):
if line > 1458705:
print(val)
break
And see what line does it actually print after the correct line number
actually I think I see the issue lol
I'm basically running the code while it's in the process of uploading I think
lmao
would this be the reason
You're running the code locally or on cloud?
oh yeah this would probably do it lmao .-. im surprised its able to read the file at all while being uploaded
why not try waiting for it to fully upload before running it
well now I know that's the reason lol
I didn't realize the code was able to run while it was uploading
and the uploading indicator is not super visible haha
anyway... mystery solved ig
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.