Open In App

File Versioning in Python

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

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.

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.

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

Article Tags :