Skip to main content

Tempfile wrapper to ensure output files are either complete, or empty. Also handles stdout, which gives no truncation guarantees.

Project description

safeoutput

Safeoutput.open returns a file handle like object which can use used to write data to disk. It is meant to be used with python's with syntax.

If an exception is thrown, safeoutput automatically deletes the file. This ensures partially rendered output is not left sitting around.

If your handle leaves scope, without an exception being thrown, the file is atomically flipped into the desired location.

Safeoutput uses temporary files to store writes, so your file contents do not have to fit in memory.

Usage

Python

import sys
import safeoutput

def calc(line):
  return line+1

input_fname = sys.argv[1]
output_fname = sys.argv[2]

with open(input_fname, 'r') as input:
  with safeoutput.open(output_fname) as output:
    for line in input.xreadlines():
      output.write(str(calc(int(line))))

Command line

$ safeoutput
usage: safeoutput [-h] [--binary] FILE

The example below ensures bar is not written until the pipe is closed.

(sleep 10; echo foo ${RANDOM} ) | \
  safeoutput --binary bar

cat bar

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safeoutput-3.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeoutput-3.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file safeoutput-3.0.tar.gz.

File metadata

  • Download URL: safeoutput-3.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for safeoutput-3.0.tar.gz
Algorithm Hash digest
SHA256 f5f8cbf28914d425d53ad841721a435fcf0fc7e14d29755a3ae74637699e3e72
MD5 52500fb0253d75dc962b0daf1aa84577
BLAKE2b-256 3d8d363eca4b879028c1d0da4aad41621208b25f3e0789d26e9fe5027e4a5f12

See more details on using hashes here.

File details

Details for the file safeoutput-3.0-py3-none-any.whl.

File metadata

  • Download URL: safeoutput-3.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for safeoutput-3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 876165650a5321f774793991af551e4e1e078c71a24f95d2e242f3f57747819c
MD5 f75aa3bfee32e70a86298ac315826477
BLAKE2b-256 23acca2cf311d1ae3b15eee9ea2136586ddc7deedb859a82bcb8d670a16307a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page