Function to copy files from one folder to another

Options
Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

How can we copy files from one directory to another?

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 810
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    Answer ✓
    Options

    The following can be used:

        import os
    
        def CopyFiles(dir1, dir2):
            '''
            Copy all files of dir1 directory into dir2 directory
            '''
            try:
                for filename in os.listdir(dir1):
                    shutil.copy(os.path.join(dir1,filename), dir2)
                return True
            except:
                ExtAPI.Log.WriteMessage("Error : Could not copy files")
                return None