Python is a strong and easy-to-learn programming language. It consists of high-level data structures and an object-oriented programming technique that is simple but effective. Python's beautiful syntax and dynamic typing, as well as its interpreted nature, make it an excellent language for scripting and quick application development across a wide range of platforms.
Some of the key features of Python include:
- Python uses white space, indentation, and scope definition
- To finish a command requires new lines and is procedural, functional, and object-oriented in nature
- Python is industry-oriented in nature, i.e., it is a cross-platform, extensible, scalable, portable programming language. It comes with support for GUI, consists of a comprehensive standard library, and allows for interactive modifications
- Python is simple to learn, manage, implement, and read for a beginner. It also has an interactive aspect to it
One of the most typical activities that a software developer does is copying files programmatically. In this article, we will be discussing a few alternative ways to copy files in Python. We will be looking at various file copying in-built methods like shutil.copy and shutil.copystat.
Python Copy File Methods
Copying files in Python has never been easier before. There are several methods related to copying files in Python. We will be discussing a couple of them in the coming topics.
Two popular Copy File Methods in Python are:
- shutil.copy()
- shutil.copystat()
These methods belong to the Shutil module of Python. So, let us first explore what the Shutil module is.
What Is the Shutil Module?
Shutil is a Python Standard Library module that provides a huge range of high-level file operations. When it comes to copying a file, the library provides a variety of options based on whether we need file permissions or we need to copy metadata, as well as if the destination is a directory.
The available in-built methods in shutil module are:
- shutil.copy
- shutil.copy2
- shutil.copyfile
- shutil.copyfileobj
- shutil.copystat
Let us now discuss two of these methods in brief with ample examples.
shutil.copy
The shutil.copy copies the supplied source (without metadata) to the chosen directory or destination file and returns the path to the newly produced file. The src parameter might be a string or a path-like object.
Syntax
shutil.copy(src, dst, *, follow_symlinks=True)
Parameters
- Return Type: A string representing the path of the newly created file is returned by this procedure
- destination: The path to the destination file or directory as a string
- source: A string that represents the source file's path
- (optional) follow symlinks: True is the default value for this parameter. If it's False and the source is a symbolic link, the destination will also be a symbolic link.
- Note: The asterisk (*) in the parameter list denotes that the following parameters (in this case, 'follow symlinks') are keyword-only parameters that can only be specified by name, not by position
Features
- Permissions on files are preserved
- A directory can be a destination
- Metadata is not copied
- It's not compatible with Objects in a file
Example1 - Copying file from Source to Destination
Code
# importing os module
import os
# importing shutil module
import shutil
# path
path = '/home/User/Simplilearn'
print("Prior to copying the file: ")
print(os.listdir(path))
# Source path storage
source_path = "/home/User/Simplilearn/sample_file.txt"
# Printing the file permission
permission = os.stat(source).st_mode
print("The File Permission mode is: ", permission, "\n")
# Destination path storage
dest_path = "/home/User/Simplilearn/sample_file(copy).txt"
# Copying content of source to destination
destination = shutil.copy(source_path, dest_path)
# Listing files and directories
print("After copying the files: ")
print(os.listdir(path))
# Printing file permissions
permission = os.stat(dest_path).st_mode
print("The File Permission mode is: ", permission)
# Printing the newly created file path
print("The Destination path:", destination)
Output
Prior to copying the file:
['rainbow.png', 'test3.py', 'sample_text.txt', 'file.text', 'copy_file.cpp']
File permission mode: 33188
After copying the files:
['rainbow.png', 'test3.py', 'sample_text.txt', 'file(copy).text', 'copy_file.cpp']
File permission mode: 33188
Destination path: /home/User/Simplilearn/sample_file(copy).txt
shutil.copystat
The shutil.copystat() method in Python is used to copy flags values, permission bits, last modification time, and last access time from one path to another.
The file content, as well as the owner and group information, are unaffected by the shutil.copystat() method.
Along with permission bits, last access time, last modification time, and flags value, this technique on Linux tries to transfer some extra properties.
Syntax
shutil.copystat(source, destination, *, follow_symlinks = True)
Parameters
source: A string that represents the source file's path.
destination: A string that represents the destination file's path.
(optional) follow symlinks: True is the default value for this parameter. If it's False and both source and destination are symbolic links, the shutil.copystat() method will work on the symbolic links themselves rather than the files they point to.
Note: The asterisk (*) in the parameter list denotes that the following parameters (in this case, 'follow symlinks') are keyword-only parameters that can only be specified by name, not by position.
Return Type: There is no value returned by this procedure.
Example
Code
import os
# importing shutil module
import shutil
# importing time module
import time
# Source file path
src = "/home/Simplilearn/Desktop/sample3.py"
# Destination file path
dest = "/home/Simplilearn/Desktop/encryption.py"
# Printing the permission bits
print("Before using shutil.copystat() method:")
print("Source metadata:")
print("Permission bits:", oct(os.stat(src).st_mode)[-3:])
print("Last access time:", time.ctime(os.stat(src).st_atime))
print("Last modification time:", time.ctime(os.stat(src).st_mtime))
# print("User defined Flags:", os.stat(src).st_flags)
# is subject to availability
print("\nDestination metadata:")
print("Permission bits:", oct(os.stat(dest).st_mode)[-3:])
print("Last access time:", time.ctime(os.stat(dest).st_atime))
print("Last modification time:", time.ctime(os.stat(dest).st_mtime))
# print("User defined Flags:", os.stat(dest).st_flags)
# Copy the permission bits
shutil.copystat(src, dest)
# Printing the permission bits
print("\nAfter using shutil.copystat() method:")
print("Destination metadata:")
print("Permission bits:", oct(os.stat(dest).st_mode)[-3:])
print("Last access time:", time.ctime(os.stat(dest).st_atime))
print("Last modification time:", time.ctime(os.stat(dest).st_mtime))
# print("User defined Flags:", os.stat(dest).st_flags)
print("Permission bits, last access time and last modification time\n\
copied from source to the destination successfully")
Output
Before using shutil.copystat() method:
Source metadata:
Permission bits: 664
Last access time: Tue March 22 00:37:16 2022
Last modification time: Tue March 22 00:18:23 2022
Destination metadata:
Permission bits: 777
Last access time: Tue March 22 00:37:16 2022
Last modification time: Tue March 22 00:18:23 2022
After using shutil.copystat() method:
Destination metadata:
Permission bits: 664
Last access time: Tue March 22 00:37:16 2022
Last modification time: Tue March 22 00:18:23 2022
Permission bits, last access time, and last modification time
copied from source to the destination successfully
Learn the essentials of object-oriented programming, web development with Django, and more with the Python Training Course. Enroll now!
Master Python With Simplilearn
File copying is a very useful tool in Python to rapidly copy files from source to destination.
We use the shutil module in Python to copy files using programming in Python.
Some of the methods in the shutil module include-
- shutil.copy
- shutil.copy2
- shutil.copyfile
- shutil.copyfileobj
- shutil.copystat
To learn all about python and grow in your programming career, enroll in Simplilearn’s Python Development Training today.