#πŸ”’ Why does this code to read the lines in a file not work?

61 messages Β· Page 1 of 1 (latest)

plain mantle
#
# 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.")
gusty jungleBOT
#

@plain mantle

Python help channel opened

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.

plain mantle
#

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

tropic lily
plain mantle
#

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

dull furnace
tropic lily
#

its the same

dull furnace
#

If you just do for line in file it'll read every character

tropic lily
#

nope

dull furnace
#

I'm pretty sure

dull furnace
plain mantle
#

pretty sure it is actually reading the lines, but it's prematurely stopping

tropic lily
#

they are the same

dull furnace
#

Hm

tropic lily
#

this one is "lazy" iteration though

#

the file is an iterator

dull furnace
#

Ig you could just do return len(file) to count the lines probably?

plain mantle
tropic lily
dull furnace
#

Damn

tropic lily
dull furnace
#

oh I see

tropic lily
#

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

plain mantle
#

I mean, this works

line_count = len(file.readlines())
tropic lily
#

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

plain mantle
#

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

plain mantle
tropic lily
#

and these are separate runtimes? not multiple function calls, but the program actually exits, and you run it again, and still get this output?

plain mantle
#

it's really weird

tropic lily
#

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

plain mantle
#

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)

dull furnace
plain mantle
#

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

dull furnace
#

You're running the code locally or on cloud?

plain mantle
#

it's google colab

#

I assume it's on the cloud?

dull furnace
#

Oh

#

yeah

tropic lily
#

why not try waiting for it to fully upload before running it

plain mantle
#

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

gusty jungleBOT
#
Python help channel closed

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.