How to move multiple ZIP files at once with Python

Asked

Viewed 280 times

3

I am creating a function to manage files using python. i would like this function to do the following: take all my zip or mp3 files (or other formats) and move them all at once to a folder.

   os.chdir('C:\\')
   shutil.copy('C:\\Users\\ODIN\\Downloads\\*.*', 'C:\\ODIN\\d')
  • 1

    Can you describe at least one of your attempts? If you press [Edit], you can change your question to add the code.

  • Have you ever googled someone has already been through this problem or something similar!

  • THANK YOU I’ll CHECK

2 answers

2

I think you’re looking for shutil.move():

shutil.move("pasta/atual/seu.doc", "nova/pasta/seu.doc")

Not only for files, but also for directories.

I recommend a read on documentation.

  • 1

    OK THANK YOU I WILL USE AND CHECK THE DOCUMENTATION

2

I believe this solution meets your need.

__author__ = '@britodfbr'

import shutil
from os import listdir
from os.path import isfile, join, basename


def move(path_origem, path_destino, ext='zip'):
    for item in [join(path_origem, f) for f in listdir(path_origem) if isfile(join(path_origem, f)) and f.endswith(ext)]:
        #print(item)
        shutil.move(item, join(path_destino, basename(item)))
        print('moved "{}" -> "{}"'.format(item, join(path_destino, basename(item))))

if __name__ == '__main__':
    move('/tmp/a', '/tmp/b')

Exit:

moved "/tmp/a/file7.zip" -> "/tmp/b/file7.zip"
moved "/tmp/a/file4.zip" -> "/tmp/b/file4.zip"
moved "/tmp/a/file0.zip" -> "/tmp/b/file0.zip"
moved "/tmp/a/file3.zip" -> "/tmp/b/file3.zip"
moved "/tmp/a/file1.zip" -> "/tmp/b/file1.zip"
moved "/tmp/a/file6.zip" -> "/tmp/b/file6.zip"
moved "/tmp/a/file9.zip" -> "/tmp/b/file9.zip"
moved "/tmp/a/file2.zip" -> "/tmp/b/file2.zip"
moved "/tmp/a/file5.zip" -> "/tmp/b/file5.zip"
moved "/tmp/a/file10.zip" -> "/tmp/b/file10.zip"
moved "/tmp/a/file8.zip" -> "/tmp/b/file8.zip"
moved "/tmp/a/file01.zip" -> "/tmp/b/file01.zip"
  • OK THANK YOU WILL BE VERY USEFUL

Browser other questions tagged

You are not signed in. Login or sign up in order to post.