Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




The shutil Module

The shutil module helps you automate copying files and directories. This saves the steps of opening, reading, writing and closing files when there is no actual processing, simply moving files.

shutil. copy ( src , dest )

Copy data and mode bits, basically the unix command cp src dst. If dest is a directory, a file with the same base name as src is created. If dest is a full file name, this is the destination file.

shutil.copyfile ( src , dest )

Copy data from src to dest . Both names must be files.

shutil.copytree ( src , dest )

Recursively copy the entire directory tree rooted at src to dest . dest must not already exist. Errors are reported to standard output.

shutil.rmtree ( path )

Recursively delete a directory tree rooted at path .

Note that this allows us to build Python applications that are like shell scripts. There are a lot of advantages to writing Python programs rather than shell scripts to automate mundane tasks.

First, Python programs are easier to read than shell scripts. This is because the language did not evolve in way that emphasized tersness; the shell script languages use a minimum of punctuation, which make them hard to read.

Second, Python programs have a more sophisticated programming model, with class definitions, and numerous sophisticated data structures. The shell works with simple argument lists; it has to resort to running the test or expr programs to process strings or numbers.

Finally, Python programs have direct access to more of the operating system's features than the shell. Generally, many of the basic GNU/Linux API calls are provided via innumerable small programs. Rather than having the shell run a small program to make an API call, Python can simply make the API call.

  Published under the terms of the Open Publication License Design by Interspire