{"id":5966,"library":"in-place","title":"In-place file processing","description":"The `in-place` library provides an `InPlace` class for robust and convenient in-place editing of files. It handles temporary files, encoding, and error management, simplifying common tasks like filtering or transforming file contents without needing to manually manage temporary files or worry about data loss during unexpected interruptions. The current version is 1.0.1 and it is actively maintained with a focus on stability and compatibility.","status":"active","version":"1.0.1","language":"en","source_language":"en","source_url":"https://github.com/jwodder/inplace","tags":["file processing","in-place editing","temporary files","file utility"],"install":[{"cmd":"pip install in-place","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The primary class for in-place file editing is `InPlace` and is typically used as a context manager.","symbol":"InPlace","correct":"import in_place\nwith in_place.InPlace('filename.txt') as fp:"}],"quickstart":{"code":"import in_place\n\n# Create a dummy file for demonstration\nwith open('example.txt', 'w') as f:\n    f.write('Line 1\\n')\n    f.write('Line 2 to be modified\\n')\n    f.write('Line 3\\n')\n\nprint('Original file content:')\nwith open('example.txt', 'r') as f:\n    print(f.read())\n\n# Modify 'example.txt' in-place: change 'Line 2' to 'Modified Line 2'\nwith in_place.InPlace('example.txt') as fp:\n    for line in fp:\n        if 'Line 2' in line:\n            fp.write(line.replace('Line 2', 'Modified Line 2'))\n        else:\n            fp.write(line)\n\nprint('\\nModified file content:')\nwith open('example.txt', 'r') as f:\n    print(f.read())","lang":"python","description":"This quickstart demonstrates how to use `in_place.InPlace` to read and write to a file, modifying its content in a transactional manner. It creates an example file, prints its original content, modifies a specific line using `in_place`, and then prints the updated content."},"warnings":[{"fix":"Always explicitly call `fp.write(line)` or `fp.write(modified_line)` within the `with in_place.InPlace(...) as fp:` block for every line you intend to keep or modify.","message":"Unlike Python's standard `fileinput` module, `in-place` does not hijack `sys.stdout` for writing. Users coming from `fileinput` must explicitly call `fp.write()` to output modified lines, or `fp.write(line)` to preserve unchanged lines, otherwise content will be lost.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Provide a non-empty string for `backup_ext` (e.g., `.bak`, `~`) or use the `backup` argument to specify a complete backup file path if you need a backup. If no backup is desired, omit both `backup` and `backup_ext`.","message":"When using `in_place.InPlace` with the `backup_ext` argument, providing an empty string for the extension will raise an error. The library explicitly prevents this to avoid ambiguity.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For binary files, instantiate `InPlace` with `mode='b'`, e.g., `with in_place.InPlace('binary_file', mode='b') as fp:`.","message":"When working with binary files, remember to open `InPlace` in binary mode (`mode='b'`). Otherwise, the file will be opened in text mode (default `mode=None`), which can lead to unexpected encoding/decoding errors or corruption of non-textual data.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z","problems":[]}