0

Can someone tell me how to write the line count on top of the file after the lines are written in that file using python?

line_count=?

sam
john
gus
heisenberg
Xdrake
  • 107
  • 5
  • There are two options. 1 -- Write it first (correctly), 2 -- rewrite the file correctly to a new file, delete the original, rename the new file to the original name. As far as the specific code, you'll need to do that. Check back here if the code has a specific problem. – Kenny Ostrom Aug 03 '21 at 18:40
  • I don't have the first option, because, the lines are written first and the number changes from file to file and I am trying to automate the code that identifies the # of lines and write it on top. – Xdrake Aug 03 '21 at 18:42
  • You'd obviously have to store it somewhere before writing the line count first. If you do that in memory, you can write it first. If you do that in the file, you'll have to re-write the file to insert something of unknown size. Although Prune's answer is pretty good (reserve a reasonable space, and I guess only re-write the file if it has more lines than that). – Kenny Ostrom Aug 03 '21 at 18:48
  • see also https://stackoverflow.com/questions/1325905/inserting-line-at-specified-position-of-a-text-file/1325927#1325927 and https://stackoverflow.com/questions/125703/how-to-modify-a-text-file – Kenny Ostrom Aug 03 '21 at 18:58
  • Isn't there a way at all to write it at the top of the file without rewriting to another file or overwriting the file? – Xdrake Aug 13 '21 at 16:43

3 Answers3

1

You can't do it quite that simply. A file is a sequence of bytes. You must first ensure that when you write all of those lines, that you write them to their final positions. The easiest way to do this is to "reserve" space for your line count when you first write the file. Much as you have done in your description, write the entire file, but keep "placeholder" room for the line count. For instance, if you have a maximum of 9999 lines in the file, then reserve four bytes for that count:

line_count=????

sam
john
gus
heisenberg

Once you finish writing all of the lines, then back up to the appropriate byte position in the file (bytes 11-14) with seek(11), and file.write() the count, formatted to four characters.

I'm assuming that you already know how to write lines to a file, and how to format an integer. All you're missing is the basic logic and the seek method.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • If the line count won't fit, you can always re-write the whole file (after you know the line count). – Kenny Ostrom Aug 03 '21 at 18:55
  • Hi thanks for the input, what if I have a completely blank row at the top, unlike the 'line_count' string here? can I still use the seek command? – Xdrake Aug 13 '21 at 15:53
  • Is there a way to do this without overwriting the lines in the file? – Xdrake Aug 13 '21 at 16:34
  • You have to either reserve the required number of bytes (hence my question marks) and overwrite, or hold the later lines until you know how much space you'll need for the first one. I don't understand how you envision filling in the line_count last, without overwriting something. Are you still confused about what a text file is? – Prune Aug 13 '21 at 20:11
1

Here ya go!

text = open("textfile.txt").readlines()
counter = 0
for row in text:
    counter += 1  # just snags the total amount of rows

newtext = open("textfile.txt","w")
newtext.write(f"line_count = {counter}\n\n") # This is your header xoxo
for row in text:
    newtext.write(row) # put all of the original lines back
newtext.close()

Make sure you put in the paths to the text file but this short script will just tack on that line_count = Number header that you wanted. Cheers!

Sand
  • 198
  • 11
  • Yeah I missed the `.readlines()`. Now the obvious question is why don't you do `counter = len(text)`? – Kelly Bundy Aug 03 '21 at 18:57
  • Oh yeah that's fine too. Might help speed things up on a really long list but I was trying to be a bit more explicit on the methodology of how it works rather than focus on brevity/optimizing – Sand Aug 03 '21 at 18:58
  • That's not about brevity/optimizing but about doing the right/obvious/straightforward thing :-) – Kelly Bundy Aug 03 '21 at 19:00
  • Thanks for the input, is there a way to just write the `number(counter)` on top, instead of `f"line_count =` ? if I remove line count it gives me a syntax error. please help – Xdrake Aug 16 '21 at 16:30
  • @Xdrake f"{counter}" should do the trick if you just want the number. Otherwise I'm not too sure what you're asking... if you could paste your code that's throwing the syntax error here I could take a look at that too :-) – Sand Aug 18 '21 at 19:06
  • Im an idiot, i thought 'f' is the file name. thank you for the answer. – Xdrake Aug 18 '21 at 19:43
  • @Xdrake Oh ha yes yes I can imagine how that'd be confusing. Glad to be of help. Cheers! – Sand Aug 18 '21 at 20:59
-1
 f.seek(0)
 f.write("line_count = %s\n"%n)
 f.close()

Where n is line count value

Mohamed ElKalioby
  • 1,908
  • 1
  • 12
  • 13