Comment fusionner deux dossiers en python ?

Created 24 novembre 2022 | Viewed 49 | by Benjamin Edit


Exemple de comment fusionner deux dossiers en python:

Introduction

J'ai utilisé google takeout pour télécharger un projet qui a été enregistré sur google drive. Cependant, comme le projet est volumineux, le projet a été divisé en deux dossiers lors du téléchargement :

Takeout_01

et

Takeout_02

dossiers.

Folders paths in my local computer:

takeout_01_path = '/Users/JDoe/Downloads/Takeout_01'
takeout_02_path = '/Users/JDoe/Downloads/Takeout_02'

L'objectif était alors de créer un code python simple pour fusionner Takeout_02 dans Takeout_01.

Itérer sur tous les fichiers et répertoires

Première étape itérer sur tous les fichiers dans Takeout_02 :

# Loop over all files from takeout_02_path

for path, subdirs, files in os.walk(takeout_02_path):

    print(path)
    print(files)
    print()

donne par exemple

/Users/JDoe/Downloads/Takeout_01/Drive/Deep_Work/ideas4eo_v2/archive/sent_to_digital_ocean/first version/ideas4eo/myproject
['__init__.py', 'settings.py', 'urls.py', 'wsgi.py']

/Users/JDoe/Downloads/Takeout_01/Drive/Deep_Work/ideas4eo_v2/archive/sent_to_digital_ocean/first version/ideas4eo/myproject/__pycache__
['settings.cpython-35.pyc', 'settings.cpython-36.pyc', 'wsgi.cpython-35.pyc', 'wsgi.cpython-36.pyc', '__init__.cpython-36.pyc', 'urls.cpython-36.pyc', '__init__.cpython-35.pyc', 'urls.cpython-35.pyc']

Vérifiez si un répertoire existe sinon créez-le

    path_tmp = path.replace(takeout_02_path,'')
    path_tmp_folder_list = path_tmp.split('/')

    del path_tmp_folder_list[0]

    if len( path_tmp_folder_list ) > 0:

        current_path = takeout_01_path

        for folder_name in path_tmp_folder_list:

            current_path += '/' + folder_name

            if not os.path.exists(current_path):

                ## Special Characters

                current_path = current_path.replace(' ', '\ ' )

                cmd = 'mkdir {}'.format(current_path)

                os.system( cmd )

Déplacer tous les fichiers de Takeout_02 vers Takeout_01

    for name in files:

        src = os.path.join(path, name)
        trg = path.replace('Takeout_02','Takeout_01') + '/.'

        ## Special Characters

        src = src.replace(' ','\ ' )
        src = src.replace('(','\(' )
        src = src.replace(')','\)' )

        trg = trg.replace(' ','\ ' )
        trg = trg.replace('(','\(' )
        trg = trg.replace(')','\)' )

        cmd = "mv {} {}".format(src,trg)

        #print('tcon',cmd)

        os.system( cmd )

Note: Si vous avez l'erreur Syntax Error Near Unexpected Token '(', pour corriger il est important de supprimer Special Characters and Citant dans les noms de fichier et de chemin.

Code complet

import os

takeout_01_path = '/Users/JDoe/Downloads/Takeout_01'
takeout_02_path = '/Users/JDoe/Downloads/Takeout_02'

# Loop over all files from takeout_02_path
for path, subdirs, files in os.walk(takeout_02_path):

    ### check if path exists in takeout_01 if not create it.

    path_tmp = path.replace(takeout_02_path,'')
    path_tmp_folder_list = path_tmp.split('/')

    del path_tmp_folder_list[0]

    if len( path_tmp_folder_list ) > 0:

        current_path = takeout_01_path

        for folder_name in path_tmp_folder_list:

            current_path += '/' + folder_name

            if not os.path.exists(current_path):

                ## Special Characters

                current_path = current_path.replace(' ', '\ ' )

                cmd = 'mkdir {}'.format(current_path)

                os.system( cmd )

    ### Move file to takeout_01

    for name in files:

        src = os.path.join(path, name)
        trg = path.replace('Takeout_02','Takeout_01') + '/.'

        ## Special Characters

        src = src.replace(' ','\ ' )
        src = src.replace('(','\(' )
        src = src.replace(')','\)' )

        trg = trg.replace(' ','\ ' )
        trg = trg.replace('(','\(' )
        trg = trg.replace(')','\)' )

        cmd = "mv {} {}".format(src,trg)

        #print('tcon',cmd)

        os.system( cmd )

        #print()

Voir aussi