Python @ DjangoSpin

Python: High-level file operations using shutil module

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

High-level file operations using shutil module in Python

High-level file operations using shutil module in Python

The standard library module shutil facilitates basic operations on files and directories. You can copy files and directories, move them, remove them, pretty much everything you would like to do with them. Following is a list of frequently used functions of the shutil module:

shutil.copy(source,destination) 						# Creates a copy of file at source path to destination path.
shutil.copymode(source,destination)						# Sets file permissions of destination file/directory to that of source file/directory.
shutil.copystat(source, destination)					# Copies permissions, statistics such as last access time, last modification time, flags of source file/directory to destination file/directory
shutil.copy2(source, destination)						# Copies the file data, permissions, metadata such as file creation & modification time etc. of source file/directory to destination file/directory
shutil.disk_usage(path)									# Returns a three-element tuple denoting total, use and free space of given path, in bytes.
shutil.move(source, destination)						# Moves the file/directory at source path to destination path. Renames the file if executed in the same directory.
shutil.copytree(source, destination)					# Copies the entire directory at source path to destination path by creating a new directory specified in destination argument.
shutil.rmtree(path)										# Removes the entire directory at given path.

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply