Common file operations – Learn Basic Python – 1

The Blog To Learn Selenium and Test Automation

Common file operations – Learn Basic Python – 1

File operations are common in application development as well as test cases automation. There are many options available in python to handle file operations; Some options are straight forward handles like importing module and call pre-defined method; Some options like os.popen() and os.system() are not straight forward.

shutil is an in-built module offering high level file operations like copying a file (or files) from one location to another location. This module is part of the standard library and included in Python since before 2.3.

In this article, we are going to see 5 most commonly used methods from shutil.

Common File Operations


  • syntax: shutil.copy(source, destination)
  • source and destination must be strings
  • copy a file to another destination file or directory
  • if destination is file, content is copied; if destination is directory file is copied with source file name
  • returns path to newly created file
  • copies the file data and the file’s permission mode
  • other metadata, like the file’s creation and modification times, is not preserved
  • if destination already exists, it will be replaced


  • syntax: shutil.copy2(source, destination)
  • same as copy but also copies the metadata of a file


  • syntax: shutil.copyfile(source, destination)
  • copy a file to another destination file
  • destination cannot be a directory here
  • IsADirectoryError error will be thrown. Example: IsADirectoryError: [Errno 21] Is a directory
  • ff source and destination specify the same file, SameFileError is raised
  • if destination already exists, it will be replaced
  • returns path to newly created file


  • syntax: shutil.copyfileobj(source_object, destination_object)
  • does the same operation as other methods but using file objects
  • good when you are editing a file and trying to copy to other location


  • syntax: shutil.copytree(source_directory, destination_directory)
  • recursively copy an entire directory tree rooted at source to destination
  • creates a directory while copying; So destination must not exist
  • FileExistsError is raised if directory exists already
  • returns the destination directory

Important things to note

  • All copy methods available in shutil module are not atomic operations. It is users responsibility to take care while using it in a threaded application
  • Copy can not handle abbreviations like ~, but can deal with relative paths
  • An IOError exception will be raised if the destination location is not writable
  • If destination already exists, it will be replaced. In some cases, error is raised. Example – copytree()
Method nameCopies Metadata?Copies permission?Accepts file object?Supports directory destination?

All shutil module methods

copy operations

  • copyfileobj
  • copyfile
  • copymode
  • copystat
  • copy
  • copy2
  • copytree

move operations

  • rmtree
  • move


  • SameFileError
  • Error

other useful methods

  • disk_usage
  • chown
  • which
  • ignore_patterns


import shutil

# target is path to filename
shutil.copy('/src_dir/old_filename.txt', '/dst_dir/new_filename.ext')

# target is a directory
shutil.copy('/src_dir/filename.txt', '/dst_dir')

# target is path to filename
shutil.copyfile('/src_dir/old_filename.txt', '/dst_dir/new_filename.ext')

# target filename is path of a file
shutil.copy2('/src_dir/old_filename.txt', '/dst_dir/new_filename.ext')

# target is a directory
shutil.copy2('/src_dir/filename.txt', '/dst_dir')

source = None
destination = None
   source = open("source_file_path", 'rb')
   destination = open("destination_file_path", 'wb')
   shutil.copyfileobj(source, destination)
except (shutil.Error, OSError, IOError) as error:
     print("Error occured - ", error)
   if source:
   if destination:

Hope this article is useful. Please let us know your thoughts in comments and you can visit all our articles here. Happy coding!!!


One Response

  1. […] visit our previous article to learn more about file operations using python. Happy […]

Leave a Reply

Your email address will not be published. Required fields are marked *