Removing specific lines in text file
So, I've been working a bit on a personal project, and mainly just for
practice as I'm just an apprentice to Python. I've learnt a lot, and I am
starting to do my own projects so that I can learn interactively and learn
a lot more.
The project I've been doing recently is basically a homework
planner/recorder, where you can input homework assignments so to say, see
all your homework assignments, clear the file, etc. Now, I've been working
on the most recent where it would remove a specific line (homework
assignment) of the file and I have no idea on how to do this the most
efficient way.
I understand there's no real way to remove a specific line from a file,
but I just can't get my hand around how I'd do it. So, for example, say I
have 5 homework assignments in the file.
1st Homework
2nd Homework
3rd Homework
4th Homework
5th Homework
Now, I want to remove the first line, so I get to the removing HW function
(by typing !delhw) and then it retrieves my homework assignments, shows me
them on the screen,a nd I gotta type in 0-4. I want to delete the first
homework assignment so I type in 0, and then it removes that line and
there we go.
Could someone explain to me a few ways I could go about this? My first
thought was I'd have to write everything in the initial file over to
another, then write it back except for the 1st line if I typed in 0 and
same for every line.
No comments:
Post a Comment