[Python] Python. How to delete file after its been moved/copied?

Discussão em 'Python' iniciado por Stack, Fevereiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I want a zip in source_dir to be copied/moved to dst location. The zip gets copied to the dist path. However, how do I get rid of the source_dir file? I tried move option, which removed the source_dir file but move command can't move a zip if theres a zip with a existing name. The copy method works and simply overwrites it but leaves a zip in source_dir. Below is my code, it copies successfully but zip file does not get removed from source_dir. What am I doing wrong?

    def Movezips(self, email):
    time.sleep(2)
    print("starting moving")
    source_dir = 'C:/Download'+email+'/'
    dst = 'E:/BackUpZip/'+email
    Path(dst).mkdir(parents=True, exist_ok=True)
    dst=dst+'/'
    files = glob.iglob(os.path.join(source_dir, "*.zip"))
    for file in files:
    if os.path.isfile(file):
    shutil.copy(file, dst)

    for file in files:
    if os.path.isfile(file):
    shutil.rmtree(file)

    Continue reading...

Compartilhe esta Página