0

I have 2 tuples.
1th tuple - files to remove (got with SQL request from DB).
2th tuple - files to save (got from html form).
Both tuples returns str file names.
I try to remove old and save new files at same time like that:

old_name = (get_image_name.img_1, get_image_name.img_2, get_image_name.img_3)
new_name = (request.files['img_1'], request.files['img_2'], request.files['img_3'])

for i in range(3):
    new_name[i].save(os.path.join(app.config['UPLOAD_FOLDER'] + 'test_folder/', new_name[i].filename))
    os.remove(os.path.join(app.config['UPLOAD_FOLDER'] + 'test_folder/', old_name[i]))

If i try to save new 2 files and remove old 2 files, code works good.
And if i try to save new 3 files and remove old 2 files, code works good.

But if i will try to save new 2 files and remove 3 old files, then 3th old file will be not remove.
How to save and remove any count of files at same time?

4
  • Why are you using a fixed range value instead of iterating over the lists? Are these two sets of files the same, i.e. are they being renamed? Jun 8, 2022 at 2:12
  • I had try to iterate over the list. Same picture. If lists the same, then code works good. But if lists are different, if list of names to remove files is bigger than list to save, then removes just count of old_name= len(new_name). Removes not all old files. Jun 8, 2022 at 11:41
  • why would the lists have different lengths if they refer to the same files? if you use fixed indices, e.g. 0, 1 and 2, then both lists need to have exactly three items. Jun 8, 2022 at 12:39
  • If i need to remove old 3 images and save at same time 2 new images, lists will be differents. 1th tuple from DB. 2th tuple from html form.. Jun 8, 2022 at 13:39

1 Answer 1

0

If the two lists are independent you will also need to iterate them separately in two loops:

old_names = (get_image_name.img_1, get_image_name.img_2, get_image_name.img_3)
new_names = (request.files['img_1'], request.files['img_2'], request.files['img_3'])

for new_name in new_names:
    new_name.save(os.path.join(app.config['UPLOAD_FOLDER'], 'test_folder', new_name.filename))

for old_name in old_names:
    os.remove(os.path.join(app.config['UPLOAD_FOLDER'], 'test_folder', old_name))

Note that I slightly changed the variable names.

2
  • I tryed like this. In this case works only first loop. If new_name loop first, then new files will be save, but old files will not removed. And vice versa. I think that because loop works in finction. And therefore i try to iterate both tuples in same loop. This function returns redirect('/dashboard'). Jun 9, 2022 at 14:00
  • @GaloGramma you didn't post the context of this code but both for loops can be located inside a function and will be run one after another, if the two input lists are available. Please post how/where you use this code. Jun 13, 2022 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.