Need some help in deleting the data from list and again append it in same list
I have developed a Django app where user can upload multiple files. I can upload all the multiple files and its paths in the form of a list separated by comma(,) in MySql database.For example I have uploaded three files
- Logging a Defect.docx, 2.Mocks (1).pptx and 3.Mocksv2.pptx
and it gets stored in database as following( Converting the individual file path into list and joining all the paths results in following form) :
FileStore/client/Logging a Defect.docx,FileStore/client/Mocks (1).pptx,FileStore/client/Mocksv2.pptx,
Now I need help while deleting particular file. For example when I'm deleting Logging a Defect.docx then I should be deleting first element of list alone and retain the other two paths. I'll be sending only name of document.
I'm retrieving the path as list and then I have to check if the name of doc being passed is there in each element of the list and if it matches then I should delete that element keeping the other elements intact. How to approach this ? It sounds like more of python question than Django question.
Use list-expression to filter the splitted text, and rebuild the string using join function
>>> db_path = 'FileStore/client/Logging a Defect.docx,FileStore/client/Mocks (1).pptx,FileStore/client/Mocksv2.pptx' >>> file_to_delete = 'Logging a Defect.docx' >>> file_separator = "," >>> new_db_path = [ ... path.strip() ... for path in db_path.split(file_separator) ... if path.strip() and file_to_delete not in path ... ] >>> string_to_save = file_separator.join(new_db_path) >>> string_to_save 'FileStore/client/Mocks (1).pptx,FileStore/client/Mocksv2.pptx'