Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
shutil.copytree() method - Python
Next article icon

Python | shutil.copystat() method

Last Updated : 07 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.
shutil.copystat() method in Python is used to copy the permission bits, last access time, last modification time, and flags value from the given source path to given destination path. 
The shutil.copystat() method does not affect the file content and owner and group information. 
On Linux, this method also try to copy some extended attributes in addition to permission bits, last access time, last modification time, and flags value.
 

Syntax: shutil.copystat(source, destination, *, follow_symlinks = True)
Parameter: 
source: A string representing the path of the source file. 
destination: A string representing the path of the destination file. 
follow_symlinks (optional) : The default value of this parameter is True. If it is False and source and destination both refers to a symbolic link then the shutil.copystat() method will operate on the symbolic links themselves rather than the files the symbolic links refer to.
Note: The ‘*’ in parameter list indicates that all following parameters (Here in our case ‘follow_symlinks’) are keyword-only parameters and they can be provided using their name, not as positional parameter.
Return Type: This method does not return any value. 
 

Code: Use of shutil.copystat() method to copy metadata from source to destination path 
 

Python3




# Python program to explain shutil.copystat() method
   
# importing os module
import os
 
# importing shutil module
import shutil
 
# importing time module
import time
 
 
# Source file path
src = "/home/ihritik/Desktop/sam3.pl"
 
 
# Destination file path
dest = "/home/ihritik/Desktop/encry.py"
 
 
# Print the permission bits
# last access time, last modification time
# and flags value of source and destination files
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)
 
# Note: st_flags attribute is platform dependent
# and 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
# last access time, last modification time
# and flags value from source to destination
shutil.copystat(src, dest)
 
 
# Print the permission bits
# last access time, last modification time
# and flags value of destination
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 destination successfully")
 
 
Output: 
Before using shutil.copystat() method: Source metadata: Permission bits: 664 Last access time: Mon Jun 10 00:37:16 2019 Last modification time: Thu Dec 27 00:15:23 2018  Destination metadata: Permission bits: 777 Last access time: Fri Apr 12 01:13:25 2019 Last modification time: Thu Apr 11 02:03:45 2019  After using shutil.copystat() method: Destination metadata: Permission bits: 664 Last access time: Mon Jun 10 00:37:16 2019 Last modification time: Thu Dec 27 00:15:23 2018  Permission bits, last access time and last modification time copied from source to destination successfully

 

Reference: https://docs.python.org/3/library/shutil.html
 



Next Article
shutil.copytree() method - Python

I

ihritik
Improve
Article Tags :
  • Python
  • python-utility
Practice Tags :
  • python

Similar Reads

  • Python | shutil.copy() method
    C/C++ Code # Python program to explain shutil.copy() method # importing shutil module import shutil # Source path source = "/home/User/Documents/file.txt" # Destination path destination = "/home/User/Documents/file.txt" # Copy the content of # source to destination try: shutil.co
    5 min read
  • Python | shutil.copy2() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.shutil.copy2() method in Python is used to copy the co
    4 min read
  • shutil.copytree() method - Python
    shutil.copytree() method in Python is used to recursively copy an entire directory tree from a source to a destination. It copies all the contents, including files and subdirectories, preserving the directory structure. Syntaxshutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2
    3 min read
  • Python | shutil.copyfile() method
    Shutil module in Python helps automate the process of copying and removing files and directories. It comes under Python’s standard utility modules.  Shutil(short for shell utility) module also provides many functions of high-level operations on files and collections of files. What is Shutil.copyfile
    4 min read
  • Python | shutil.copymode() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removal of files and directories.shutil.copymode() method in Python is used to copy
    2 min read
  • Python | shutil.chown() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of chowning and removal of files and directories.shutil.chown() method in Python is used to change the
    3 min read
  • Python | shutil.copyfileobj() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of chowning and removal of files and directories.shutil.copyfileobj() method in Python is used to copy
    2 min read
  • Python List copy() Method
    The copy() method in Python is used to create a shallow copy of a list. This means that the method creates a new list containing the same elements as the original list but maintains its own identity in memory. It's useful when you want to ensure that changes to the new list do not affect the origina
    3 min read
  • Python | shutil.move() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating the process of copying and removing files and directories. The shutil.move() method moves a file or directory
    2 min read
  • Python | shutil.disk_usage() method
    Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories. shutil.disk_usage() method tells the disk usage stati
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences