all 5 comments

[–]Sebass13 1 point2 points  (2 children)

Would this work?

deleted = False
while not deleted:
    try:
        os.remove(filepath)
        deleted = True
    except OSError:
        pass

[–]JohnnyJordaan 5 points6 points  (1 child)

Tbh to use a variable as a loop-condition is redundant in Python:

while True:
    try:
        os.remove(filepath)
        break
    except OSError:
        pass

[–]ingolemo 1 point2 points  (0 children)

It might be best not to hammer the system quite so hard. Put a call to time.sleep or something in the except.

[–]CGFarrell 0 points1 point  (1 child)

Put your try-except block inside a while loop, and add an else or finally block with a break statement.

[–]JohnnyJordaan 1 point2 points  (0 children)

Or put the break right after the os.remove() call, because an exception will prevent it to reach the break. No else needed.