commoncode Utilities

32.4.2 · active · verified Fri Apr 17

commoncode is a collection of essential utilities for Python, originally split from the ScanCode project. It provides robust tools for file system operations, hashing, system information, command execution, and more. It is actively maintained with frequent minor releases to support new Python versions and address bug fixes. The current version is 32.4.2.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `commoncode.fileutils.file_sha1` to compute a file's SHA1 checksum and `commoncode.system.get_system_info` to retrieve basic system details. It creates a temporary file and cleans it up afterward.

import os
from pathlib import Path
from commoncode.fileutils import file_sha1

# Create a dummy file for demonstration
dummy_file_path = Path('dummy_file.txt')
with open(dummy_file_path, 'w') as f:
    f.write('Hello, commoncode!\n')
    f.write('This is a test file.\n')

try:
    # Get SHA1 checksum of the file
    sha1_checksum = file_sha1(str(dummy_file_path))
    print(f"SHA1 of '{dummy_file_path}': {sha1_checksum}")

    # Get system information
    from commoncode.system import get_system_info
    system_info = get_system_info()
    print(f"Python Version: {system_info.get('python_version')}")
    print(f"OS Release: {system_info.get('os_release_id')}")

finally:
    # Clean up the dummy file
    if dummy_file_path.exists():
        os.remove(dummy_file_path)

view raw JSON →