How to move multiple ZIP files at once with Python

1

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

   os.chdir('C:\')
   shutil.copy('C:\Users\ODIN\Downloads\*.*', 'C:\ODIN\d')
    
asked by anonymous 19.06.2018 / 01:47

2 answers

0

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

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

Not just for files, but also for directories.

I recommend reading the documentation .

    
19.06.2018 / 14:10
0

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')

Output:

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"
    
19.06.2018 / 15:07