Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly Implement Shutil.Error in Python

I'm learning Python 3 and trying to write a script that will copy a directory. I'm using shutil.copytree. From the Python documentation it says:

If exception(s) occur, an Error is raised with a list of reasons.

This exception collects exceptions that are raised during a multi-file operation. For copytree(), the exception argument is a list of 3-tuples (srcname, dstname, exception).

In the example they do this:

 except Error as err:
            errors.extend(err.args[0])

Here is my script:

def copyDirectory(src, dest):

    errors = []

    try:
        shutil.copytree(src, dest)
     except Error as err:
            errors.extend(err.args[0])

source="C:/Users/MrRobot/Desktop/Copy"      
destination="C:/Users/MrRobot/Desktop/Destination"

copyDirectory(source, destination)
moveDirectory(destination,"I:/")

Questions:

  1. How do you properly catch an exception that might occur when using shutil.copytree (assuming my above script is incorrect)?

  2. How then would you view the errors that occurred, would I loop through the errors array?

like image 372
Emma Geller-Green Avatar asked Nov 08 '15 16:11

Emma Geller-Green


People also ask

What is the use of shutil in Python?

Shutil module offers high-level operation on a file like a copy, create, and remote operation on the file. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories. In this article, we will learn this module.

What is the use of shutil () method in CMD?

This module helps in automating process of copying and removal of files and directories. shutil.which () method tells the path to an executable application which would be run if the given cmd was called. This method can be used to find a file on computer which is present on the PATH.

How to get the path to an executable application using shutil?

import shutil import os path = 'D:\DSCracker\DS Cracker\NewPython\python1' statistics=shutil.disk_usage (path) print (statistics) usage (total=1000203087872, used=9557639168, free=990645448704) shutil.which () function returns the path to an executable application which would run if the given command cmd was called.

What is the exception argument in shutil copytree?

I'm using shutil.copytree. From the Python documentation it says: If exception (s) occur, an Error is raised with a list of reasons. This exception collects exceptions that are raised during a multi-file operation. For copytree (), the exception argument is a list of 3-tuples (srcname, dstname, exception).


Video Answer


1 Answers

You need to either include the module name when you catch the exception:

except shutil.Error as err:

Or import it explicitly:

from shutil import copytree, Error

# the rest of your code...

try:
    copytree(src, dest)
 except Error as err:
        errors.extend(err.args[0])

To view the traceback and exception information, you have a few options:

  1. Don't catch the exception. Your script will be halted and all the error information will be printed.

  2. If you want the script to continue, then you're really asking a duplicate of this SO question. I would reference that question; the accepted answer is written very well.

And by the way, you should avoid calling it an array. This particular exception object has a list of tuples, and arrays are an entirely different data structure.

like image 121
skrrgwasme Avatar answered Oct 17 '22 18:10

skrrgwasme