+ 2

Python — File Handling

Hello, I need help with file handling in python. Let's assume I have 2 function: add() And dela() First function is responsible for adding new task to the file. Second function is responsible for deleting task from a file. The code for these is shown here: https://code.sololearn.com/ciPtSPmRXjG5/?ref=app When user uses add() function, the text will be added in the following format: "Task *number* : *user input* " This works indeed. When user uses dela() function, the program will delete the task with the specified task number from the user input. So it will read lines, and loop through them to see if user input is present. However, when task is deleted, the order in which tasks are numbered breaks down. If test.txt file looks like this: #reserved line Task 1 : Make this program Task 2 : Make another program Task 3 : Learn this If I delete task 2, and add another task, test.txt will look like this: #reserved line Task 1 : Make this program Task 3 : Learn This Task 3 : Added task

3rd Feb 2023, 1:05 PM
Lamron
Lamron - avatar
18 Respuestas
+ 10
Lamron , have a look to the attached file, that describes the steps how to handle this task. https://code.sololearn.com/cV40Jpk3aKhr/?ref=app
3rd Feb 2023, 4:19 PM
Lothar
Lothar - avatar
+ 8
Marijke van Bommel , 1) if you are reading my description, you can see that it will process correctly *deletings* and *insertions / appends* 2) also notice that the description uses range or enumerate which are creating numbers that are used as indexes. this a rough attempt: https://code.sololearn.com/cVr15LzZfGpm/?ref=app
4th Feb 2023, 8:40 PM
Lothar
Lothar - avatar
+ 3
I think that the easiest method is to change how your functions work. Your functions open files and change them. You can creating list that will contain all of your tasks and you can use list indexing for tasks' numbers. You can create another function that will rewrite your file using list with tasks and you can call tak function every time you will make change to the list.
3rd Feb 2023, 1:19 PM
Jan J
Jan J - avatar
+ 2
The second one rearranges the task numbers when delete. It leaves an extra newline at the end though. https://code.sololearn.com/cZ2kNxU9pEdO/?ref=app https://code.sololearn.com/cfKzbfvOj5Ck/?ref=app
3rd Feb 2023, 3:27 PM
Lochard
Lochard - avatar
+ 2
if task_remove not in line: This breaks down if the task includes a number. Task 1 : Complete 2 lessons Task 2 : Have fun Task 3 : Sleep Deleting task 2 will delete task 1 as well.
4th Feb 2023, 6:25 AM
Marijke van Bommel
Marijke van Bommel - avatar
+ 2
Lothar's idea can be simplified by using list indexing. Ignore the task numbering that's in the nested list, and rebuild the file similar to how you added a task in your original code, but use the index instead of len(x). You can delete a task using the index, as well.
4th Feb 2023, 6:51 AM
Marijke van Bommel
Marijke van Bommel - avatar
+ 1
I forgot that using my method you have to load the file at the beginning of your program. You can use readlines to read all lines in file and next you can use split for each element of the list to separate task number and what the task is
3rd Feb 2023, 1:25 PM
Jan J
Jan J - avatar
+ 1
There are some small differences between lines that would be written by your code and your sample text. 1. your code is using semi-colon instead of colon. 2. it adds a space before the semi-colon. It is relatively simpler with the text format of your code since it makes the number an element on its own when split by whitespaces. You can simply change the assignment of y to get the number of that element plus 1. That is, if you don't need to reuse the numbers of the deleted tasks. Edit: Seems that I have read it wrong, your sample text also have a space before the colon.
3rd Feb 2023, 1:51 PM
Lochard
Lochard - avatar
+ 1
I don't seem to understand neither of those solutions. I have tried using lists, everything works, but again I don't understand how I can keep the order of the tasks.
3rd Feb 2023, 2:04 PM
Lamron
Lamron - avatar
+ 1
Lamron Seems that I have misread your sample text, there are spaces before colons. simply change the assignment for y to: y = int(x[-1].split()[1]) + 1
3rd Feb 2023, 2:13 PM
Lochard
Lochard - avatar
+ 1
Thanks for guidance everyone. I'll look at it when I'm back to that small program!🙂
3rd Feb 2023, 6:04 PM
Lamron
Lamron - avatar
+ 1
I tried, it works! Thanks. Sorry for a late reply
10th Feb 2023, 2:30 PM
Lamron
Lamron - avatar
0
What can I do to keep task numeration in order?
3rd Feb 2023, 1:05 PM
Lamron
Lamron - avatar
0
Hmmm, let me try that
3rd Feb 2023, 1:20 PM
Lamron
Lamron - avatar
0
Just trying to use list methods to see if it can work
3rd Feb 2023, 1:32 PM
Lamron
Lamron - avatar
0
Lochard can you please expand on your method?
3rd Feb 2023, 2:04 PM
Lamron
Lamron - avatar
0
I'll do this piece of code later. Just need to take a break 🙂
3rd Feb 2023, 2:20 PM
Lamron
Lamron - avatar
0
Mirielle May I ask what OS you are using? Btw, I am using the replit mobile app to test.
3rd Feb 2023, 3:35 PM
Lochard
Lochard - avatar