{"id":3500,"date":"2026-11-30T13:13:25","date_gmt":"2026-11-30T04:13:25","guid":{"rendered":"https:\/\/danlabs.kr\/?p=3500"},"modified":"2026-11-30T13:13:43","modified_gmt":"2026-11-30T04:13:43","slug":"python-os-replace-json-20261130","status":"publish","type":"post","link":"https:\/\/danlabs.kr\/?p=3500","title":{"rendered":"Python os.replace: \uc4f0\ub2e4 \ub9cc JSON\uc744 \uacf5\uac1c\ud558\uc9c0 \uc54a\ub294 \ubc95"},"content":{"rendered":"<p>\uc7ac\uace0 \uc9d1\uacc4 \uc2a4\ud06c\ub9bd\ud2b8\uac00 <code>stock.json<\/code>\uc744 \uac31\uc2e0\ud558\ub2e4 \uba48\ucd94\uba74 \ub2e4\uc74c \uc791\uc5c5\uc740 \uc5b4\ub5a4 \ud30c\uc77c\uc744 \uc77d\uc744\uae4c\uc694? \uacf5\uac1c \uacbd\ub85c\ub97c <code>w<\/code>\ub85c \uc5f4\uba74 \uc0c8 \uc9d1\uacc4\uac00 \uc644\uc131\ub418\uae30 \uc804\uc5d0 \uae30\uc874 \ub0b4\uc6a9\ubd80\ud130 \uc798\ub824\uc694.<sup><a href=\"#source-3\">[3]<\/a><\/sup> \uc77d\ub294 \ucabd\uc774 \uadf8\ub3d9\uc548 \uc774\uc804 \uacb0\uacfc\ub97c \uc4f8 \uc218 \uc788\ub3c4\ub85d, \uc784\uc2dc \uacbd\ub85c\uc5d0 \uc0c8 \uacb0\uacfc\ub97c \ub2e4 \uc4f4 \ub2e4\uc74c Python\uc758 <code>os.replace()<\/code>\ub85c \uacf5\uac1c \ud30c\uc77c\uc744 \uad50\uccb4\ud574\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup><\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-inline1.jpg\" alt=\"\uacf5\uac1c \ud30c\uc77c\uc5d0 \ubc14\ub85c \uc4f0\ub2e4 \uc2e4\ud328\ud558\uba74 \uae30\uc874 \ubb38\uc11c \ub300\uc2e0 \ubbf8\uc644\uc131 \ubb38\uc11c\uac00 \ub0a8\ub294 \uc0dd\uc131 \ub3c4\uc2dd\" width=\"1280\" height=\"720\" loading=\"lazy\"><\/figure>\n<h2>\ub36e\uc5b4\uc4f0\uae30\ub294 JSON\uc774 \uc644\uc131\ub418\uae30 \uc804\uc5d0 \uc2dc\uc791\ub3fc\uc694<\/h2>\n<p>\uc608\uc81c\ub294 \uc7ac\uace0\uac00 7\uc778 \ud30c\uc77c \ub450 \uac1c\ub97c \ub9cc\ub4e0 \ub4a4 \uc9c1\ub82c\ud654\ud560 \uc218 \uc5c6\ub294 \uac12\uc744 \ub123\uc5b4 \ub450 \ubc29\uc2dd\uc758 \uc2e4\ud328\ub97c \ube44\uad50\ud574\uc694. <code>open('w')<\/code>\ub294 <code>json.dump()<\/code>\uc5d0 \ub4e4\uc5b4\uac00\uae30 \uc804\uc5d0 \uae30\uc874 \ub0b4\uc6a9\uc744 \uc9c0\uc6b0\ubbc0\ub85c, \ud14c\uc2a4\ud2b8\uc5d0\uc120 \ud30c\uc77c\uc5d0 \uc0c8 JSON\uc758 \ud0a4\uae4c\uc9c0\ub9cc \ub0a8\uc558\uc5b4\uc694.<sup><a href=\"#source-3\">[3]<\/a><\/sup><\/p>\n<h2>\uc784\uc2dc \ud30c\uc77c\uc740 \ubaa9\uc801\uc9c0 \uc606\uc5d0 \ub9cc\ub4e4\uc5b4\uc694<\/h2>\n<p>\ud30c\uc77c\uc2dc\uc2a4\ud15c\uc744 \uac74\ub108\ub294 \uad50\uccb4\ub294 \uc2e4\ud328\ud560 \uc218 \uc788\uc5b4\uc11c, <code>NamedTemporaryFile()<\/code>\uc5d0 <code>dir=path.parent<\/code>\ub97c \ub118\uaca8 \uc784\uc2dc \ud30c\uc77c\uc744 \ubaa9\uc801\uc9c0\uc640 \uac19\uc740 \ub514\ub809\ud130\ub9ac\uc5d0 \ub46c\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup><sup><a href=\"#source-2\">[2]<\/a><\/sup> \uae30\ubcf8\uac12\uc73c\ub85c \ub9cc\ub4e4\uba74 \ub2eb\uc744 \ub54c \ud30c\uc77c\uc774 \uc9c0\uc6cc\uc9c0\ubbc0\ub85c <code>delete=False<\/code>\ub85c \ub2eb\uc740 \ub4a4\uc5d0\ub3c4 \uc784\uc2dc \uacbd\ub85c\ub97c \ub0a8\uaca8\uc694.<sup><a href=\"#source-2\">[2]<\/a><\/sup> <code>with<\/code>\uc5d0\uc11c \ub098\uc640 \uc2a4\ud2b8\ub9bc\uc744 \ub2eb\uc740 \ub4a4 \uad50\uccb4\ud558\uace0, \uc2e4\ud328\ud574 \ub0a8\uc740 \uc784\uc2dc \ud30c\uc77c\uc740 <code>finally<\/code>\uc5d0\uc11c \uc9c0\uc6cc\uc694.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-inline2.jpg\" alt=\"\uc791\uc131 \uc911\uc778 \uc784\uc2dc \ud30c\uc77c\uacfc \uadf8\ub300\ub85c \ub0a8\uc740 \uacf5\uac1c \ud30c\uc77c\uc744 \ub098\ub780\ud788 \ubcf4\uc5ec \uc8fc\ub294 \uc0dd\uc131 \ub3c4\uc2dd\" width=\"1280\" height=\"720\" loading=\"lazy\"><\/figure>\n<pre><code class=\"language-python\">import json\nimport os\nfrom pathlib import Path\nfrom tempfile import NamedTemporaryFile, TemporaryDirectory\n\n\ndef write_json(path, data):\n    staged = None\n    try:\n        with NamedTemporaryFile(\n            mode='w', encoding='utf-8', dir=path.parent, delete=False\n        ) as stream:\n            staged = Path(stream.name)\n            json.dump(data, stream, ensure_ascii=False)\n        os.replace(staged, path)\n    finally:\n        if staged is not None:\n            staged.unlink(missing_ok=True)\n\n\nif __name__ == '__main__':\n    with TemporaryDirectory() as directory:\n        direct = Path(directory) \/ 'direct.json'\n        staged = Path(directory) \/ 'stock.json'\n        old = {'stock': 7}\n        for path in (direct, staged):\n            path.write_text(json.dumps(old), encoding='utf-8')\n        bad = {'stock': object()}\n\n        try:\n            with direct.open('w', encoding='utf-8') as stream:\n                json.dump(bad, stream)\n        except TypeError:\n            print('direct:', repr(direct.read_text(encoding='utf-8')))\n\n        try:\n            write_json(staged, bad)\n        except TypeError:\n            assert json.loads(staged.read_text(encoding='utf-8')) == old\n            assert set(Path(directory).iterdir()) == {direct, staged}\n            print('after failure:', staged.read_text(encoding='utf-8'))\n\n        write_json(staged, {'stock': 8})\n        assert json.loads(staged.read_text(encoding='utf-8')) == {'stock': 8}\n        assert set(Path(directory).iterdir()) == {direct, staged}\n        print('after replace:', staged.read_text(encoding='utf-8'))<\/code><\/pre>\n<p>Linux\uc640 Python 3.14.9\uc5d0\uc11c \uc2e4\ud589\ud588\uc5b4\uc694. <code>stock.json<\/code>\uc740 \uc2e4\ud328 \ub4a4 \uc7ac\uace0 7, \uad50\uccb4 \ub4a4 8\uc744 \ub2f4\uc558\uace0 \ub450 \uacbd\uc6b0 \ubaa8\ub450 \uc784\uc2dc \ud30c\uc77c\uc740 \ub0a8\uc9c0 \uc54a\uc558\uc5b4\uc694.<\/p>\n<pre><code class=\"language-text\">direct: '{\"stock\": '\nafter failure: {\"stock\": 7}\nafter replace: {\"stock\": 8}<\/code><\/pre>\n<h2>\uc6d0\uc790\uc801 \uad50\uccb4\ub294 \uacf5\uac1c \uc2dc\uc810\uc5d0 \ub300\ud55c \uc57d\uc18d\uc774\uc5d0\uc694<\/h2>\n<p>Python \uacf5\uc2dd \ubb38\uc11c\uc5d0\uc11c <code>os.replace()<\/code>\ub294 \uad8c\ud55c\uc774 \uc788\uc73c\uba74 \uae30\uc874 \ub300\uc0c1 \ud30c\uc77c\uc744 \ubc14\uafb8\uace0, \uc131\uacf5\ud55c \uc774\ub984 \uad50\uccb4\ub294 \uc6d0\uc790\uc801\uc774\ub77c\uace0 \uc124\uba85\ud574\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> \uc608\uc81c\uc5d0\uc11c\ub294 \uacf5\uac1c \uacbd\ub85c\ub97c \uc0c8\ub85c \uc5ec\ub294 \ucabd\uc5d0 \uc791\uc131 \uc911\uc778 \uc784\uc2dc \ud30c\uc77c\uc774 \ubcf4\uc774\uc9c0 \uc54a\ub294\ub2e4\ub294 \ub73b\uc774\uc5d0\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> \ub300\ud45c \uc774\ubbf8\uc9c0\uc640 \ubcf8\ubb38 \uadf8\ub9bc\uc740 \uc774 \ud750\ub984\uc744 \ub098\ud0c0\ub0b8 \uc0dd\uc131 \uac1c\ub150 \uc774\ubbf8\uc9c0\uc608\uc694.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-inline3.jpg\" alt=\"\uc644\uc131\ub41c \uacb0\uacfc \ud30c\uc77c\uc774 \uacf5\uac1c \uc704\uce58\uc5d0 \ub4e4\uc5b4\uac04 \ubaa8\uc2b5\uc744 \ubcf4\uc5ec \uc8fc\ub294 \uc0dd\uc131 \ub3c4\uc2dd\" width=\"1280\" height=\"720\" loading=\"lazy\"><\/figure>\n<p>\uacf5\uac1c \uacbd\ub85c\uc758 \uad50\uccb4\uc640 \ub514\uc2a4\ud06c \uae30\ub85d\uc740 \ubcc4\uac1c\ub77c\uc11c, \ubc84\ud37c\uac00 \uc788\ub294 \ud30c\uc77c\uc758 \ub0b4\uc6a9\uc744 \ub514\uc2a4\ud06c\uc5d0 \uc4f8 \ub54c Python \ubb38\uc11c\uac00 \uc548\ub0b4\ud558\ub294 \uc21c\uc11c\ub294 <code>flush()<\/code> \ub2e4\uc74c <code>os.fsync()<\/code>\uc608\uc694.<sup><a href=\"#source-1\">[1]<\/a><\/sup> \uc608\uc81c\ub294 \uadf8 \ud638\ucd9c\uc744 \ud558\uc9c0 \uc54a\uc73c\uba70, \uc804\uc6d0 \uc7a5\uc560 \ub4a4 \ubcf5\uad6c\ub098 \uc5ec\ub7ec \uc791\uc131\uc790\uac00 \ub3d9\uc2dc\uc5d0 \uac31\uc2e0\ud558\ub294 \uc0c1\ud669\ub3c4 \ub2e4\ub8e8\uc9c0 \uc54a\uc544\uc694.<\/p>\n<p>\uad00\ub828 \uae00: <a href=\"https:\/\/danlabs.kr\/?p=3428\">Python ExitStack\uc73c\ub85c \uc5ec\ub7ec \uc785\ub825 \ud30c\uc77c \uc5f4\uae30<\/a><\/p>\n<p><strong>Sources<\/strong><\/p>\n<ol>\n<li id=\"source-1\"><a href=\"https:\/\/docs.python.org\/3.14\/library\/os.html\" target=\"_blank\" rel=\"noopener noreferrer\">Python 3.14 \uacf5\uc2dd \ubb38\uc11c: os.replace (2026-11-30 \uc870\ud68c)<\/a><\/li>\n<li id=\"source-2\"><a href=\"https:\/\/docs.python.org\/3.14\/library\/tempfile.html\" target=\"_blank\" rel=\"noopener noreferrer\">Python 3.14 \uacf5\uc2dd \ubb38\uc11c: NamedTemporaryFile (2026-11-30 \uc870\ud68c)<\/a><\/li>\n<li id=\"source-3\"><a href=\"https:\/\/docs.python.org\/3.14\/library\/functions.html\" target=\"_blank\" rel=\"noopener noreferrer\">Python 3.14 \uacf5\uc2dd \ubb38\uc11c: open (2026-11-30 \uc870\ud68c)<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Python os.replace\ub85c JSON\uc744 \uc4f8 \ub54c \uae30\uc874 \ud30c\uc77c\uc744 \uc9c0\ud0a4\ub294 \ubc29\ubc95\uc774\uc5d0\uc694. \uc784\uc2dc \ud30c\uc77c\uc5d0\uc11c \uc9c1\ub82c\ud654\ub97c \ub05d\ub0b8 \ub4a4 \uad50\uccb4\ud558\ub294 \ucf54\ub4dc\uc640 \uc2e4\ud328 \uc804\ud6c4 \ucd9c\ub825\uc744 \ud568\uaed8 \ud655\uc778\ud574\uc694.<\/p>\n","protected":false},"author":1,"featured_media":3496,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-3500","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"featured_image_urls":{"full":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat.jpg",1280,720,false],"thumbnail":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat-150x150.jpg",150,150,true],"medium":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat-300x169.jpg",300,169,true],"medium_large":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat-768x432.jpg",640,360,true],"large":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat-1024x576.jpg",640,360,true],"1536x1536":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat.jpg",1280,720,false],"2048x2048":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat.jpg",1280,720,false],"morenews-large":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat-825x575.jpg",825,575,true],"morenews-medium":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat-590x410.jpg",590,410,true],"danlabs-list-small":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat-366x190.jpg",366,190,true],"danlabs-list-retina":["https:\/\/danlabs.kr\/wp-content\/uploads\/2026\/11\/python-os-replace-json-20261130-feat.jpg",1280,720,false]},"author_info":{"display_name":"DanLABs","author_link":"https:\/\/danlabs.kr\/author\/hello-danlabs-kr"},"category_info":"<a href=\"https:\/\/danlabs.kr\/?cat=19\" rel=\"category\">\uac1c\ubc1c<\/a>","tag_info":"\uac1c\ubc1c","comment_count":"0","_links":{"self":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts\/3500","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3500"}],"version-history":[{"count":1,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts\/3500\/revisions"}],"predecessor-version":[{"id":3501,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/posts\/3500\/revisions\/3501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=\/wp\/v2\/media\/3496"}],"wp:attachment":[{"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3500"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3500"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danlabs.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3500"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}