Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
so yes, here is initial file generated by python:

abcdef
qwerty

lorum ispum....



and few things i want to do with it:
1) delete line with
qwerty

2) edit empty line with
123456789

3) insert line between
abcdef
and
123456789
with content
somethingsomething


is it even possible in python? or c? (i can move project to other language btw)

yes i was googling but no i dont want to put it into ram.

btw these things will just help me doing a bigger project and above thing is just example (irl im gonna write blog engine... code here : engine/generator.py at main · hacknorris-aka-penguin/engine · GitHub[^])

no im not doing for educational or work reasons, im an indie dev...



What I have tried:

https://stackoverflow.com/questions/10507230/insert-line-at-middle-of-file-with-python[^]
https://stackoverflow.com/questions/4719438/editing-specific-line-in-text-file-in-python[^]
and few other blogs but all were based on stackoverflow answers (loading into ram)
Posted
Updated 1-Oct-22 6:04am
v2
Comments
Member 8428760 10-Oct-22 16:50pm    
Have you tried pandas with dataframe? You might be able pull the lines in as a table column, then modify it and save it back out.

No, it's not possible, unless you go via memory in one way or another.

Let me explain why: A text file is not line based - it's a binary file that is processed by applications which interpret the binary values as printable and unprintable characters. In the first class are the letters, number, punctuation, special characters (and in Unicode files an enormous number of other characters and language character sets).
And in the unprintable characters are some which the app to interprets as an "end of line character".

So when you try to insert or delete lines in a file, you can't because files don't "know" about lines. The only ways to do it are to read the whole file into memory, modify the memory, and write it back - or read it line by line and write it out to a new file line by line, adding and removing lines as you go through by writing extras to the new file for an "insert", or not writing them at all for a "delete". When you have copied the file over, you can then close them both, delete the original, and rename the new version.
 
Share this answer
 
v2
Why not start at the obvious place:
7. Input and Output — Python 3.10.7 documentation[^].

[edit]
And here is a great tutorial that shows how to create a blog web application:
Tutorial — Flask Documentation (2.2.x)[^]
[/edit]
 
Share this answer
 
v2
Comments
hacknorris-dev 1-Oct-22 11:58am    
well... not this type of blog... this type which wouldnt display anything in on page ;p (maybe one message to look into other place but its all... )
Richard MacCutchan 1-Oct-22 12:40pm    
Obviously.
hacknorris-dev 1-Oct-22 13:23pm    
was also thinking about json but then how i'd parse all content in js regardless of number of posts?
Richard MacCutchan 1-Oct-22 15:17pm    
You can think about anything you like. But if you want assistance then you need to post a properly detailed question - we cannot read your mind.
hacknorris-dev 1-Oct-22 16:41pm    
i just wanted to say that flask cant be used for this project. just look at existing source, okay? imagine someone loading his 3 year biography into ram...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900