Open In App

File Versioning in Python

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, the term “file versioning” usually refers to the process of keeping track of several file versions, frequently recording changes, preserving the past, and promoting cooperation in software development projects. In this article, we will what is file versioning in Python and how we can use it.

What is File Versioning?

File versioning in Python refers to the practice of managing and tracking changes made to files over time. This process involves creating and maintaining multiple versions of a file, allowing users to revert to previous states if necessary. File versioning is commonly used in software development for source code files and other types of files critical to the development process.

Advantages of File Versioning in Python

  • Backup and Recovery: Ensures data integrity and minimizes data loss risks by maintaining multiple file versions for easy reversion in case of accidental deletion.
  • Collaboration: Facilitates seamless teamwork with a centralized file repository, enabling concurrent work, change tracking, and conflict reduction among team members.
  • Audit Trail: Provides transparency and accountability through timestamped versions with metadata, simplifying the tracking of modifications and authorship for a thorough review.
  • Experimentation and Rollback: Allows for safe experimentation with different changes and configurations, enabling quick reversion to previous versions in case of unexpected outcomes without compromising stability.

File Versioning in Python

Below, are examples of how to File Versioning in Python:

File Structure

Here, is the file structure before versioning of the file.

1b

Example 1: Python File Versioning Using Shutil Module

In this example, below Python code uses shutil, os, and datetime to create a versioned copy of a file with a timestamp appended to its name. It notifies the user if the file doesn’t exist. This method is useful for quickly creating file versions for backup and change tracking purposes.

Python3
import shutil
import os
import datetime

def version_file(file_path):
    if os.path.exists(file_path):
        timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        versioned_file = f"{file_path}.{timestamp}"
        shutil.copyfile(file_path, versioned_file)
        print(f"Version created: {versioned_file}")
    else:
        print("File not found.")

# Example usage:
file_path = "example.txt"
version_file(file_path)

Output

Version created: example.txt.20240319112627

1a

Example 2: Python File Versioning using JSON metadata

In this example, below Python code creates a versioned copy of a file with metadata saved in a JSON format. It appends a timestamp to the filename, copies the original file with this timestamp, and saves metadata such as the original file path and timestamp in a JSON file. If the specified file doesn’t exist, it notifies the user accordingly.

Python3
import json
import os
import shutil
import datetime

def version_file_with_metadata(file_path):
    if os.path.exists(file_path):
        timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        versioned_file = f"{file_path}.{timestamp}"
        shutil.copyfile(file_path, versioned_file)

        # Save metadata
        metadata = {"original_file": file_path, "timestamp": timestamp}
        metadata_file = f"{versioned_file}.json"
        with open(metadata_file, "w") as f:
            json.dump(metadata, f)

        print(f"Version created: {versioned_file}")
        print(f"Metadata saved: {metadata_file}")
    else:
        print("File not found.")

# Example usage:
file_path = "example.txt"
version_file_with_metadata(file_path)

Output:

Version created: example.txt.20240319112856
Metadata saved: example.txt.20240319112856.json

2a



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads