{"id":414,"date":"2019-01-04T18:10:53","date_gmt":"2019-01-04T09:10:53","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=414"},"modified":"2021-06-12T14:30:15","modified_gmt":"2021-06-12T05:30:15","slug":"python-with","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/01\/04\/python-with\/","title":{"rendered":"Python\u3067\u306ewith\u306e\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u8a73\u7d30"},"content":{"rendered":"<h2 class=\"title-border\"><strong>with\u306e\u57fa\u672c<\/strong><\/h2>\n<p>\u3053\u3053\u3067\u306f\u3001python\u306ewith\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u8a18\u8f09\u3059\u308b\u3002<br \/>\nwith\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3002<br \/>\n<span class=\"pink_span\"><br \/>\nwith \u30af\u30e9\u30b9 as a:<br \/>\n\u00a0 \u00a0\u51e6\u7406<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>with\u306f\u30d5\u30a1\u30a4\u30eb\u5165\u51fa\u529b\u306a\u3069\u306e\u969b\u306b\u3088\u304f\u7528\u3044\u3089\u308c\u308b\u3002<br \/>\nwith\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3068\u5229\u7528\u3057\u306a\u3044\u5834\u5408\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u9055\u3044\u304c\u3042\u308b\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>with\u3092\u5229\u7528\u3057\u306a\u3044\u5834\u5408<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfile = open('sample.txt', mode='w') <strong># \u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u51e6\u7406<\/strong><br \/>\nfile.write('Hello') <strong># \u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304d\u8fbc\u3080\u51e6\u7406<\/strong><br \/>\nfile.close() <strong># \u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u51e6\u7406<\/strong>\n<\/div>\n<ul>\n<li>with\u3092\u5229\u7528\u3059\u308b\u5834\u5408<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nwith open('sample.txt', mode='w') as file:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;file.write('Hello') <strong># \u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304d\u3001\u66f8\u304d\u8fbc\u307f\u3001\u9589\u3058\u308b\u4e00\u9023\u306e\u51e6\u7406\u3092\u884c\u3046<\/strong>\n<\/div>\n<\/div>\n<p>with\u3092\u5229\u7528\u3057\u3066\u30d5\u30a1\u30a4\u30eb\u306e\u5165\u51fa\u529b\u3092\u3057\u305f\u3089\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b(close)\u3059\u308b\u51e6\u7406\u304c\u81ea\u52d5\u7684\u306b\u5b9f\u884c\u3055\u308c\u308b\u3002<br \/>\n\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u51e6\u7406\u3092\u5fd8\u308c\u308b\u3068\u3001\u30e1\u30e2\u30ea\u304c\u89e3\u653e\u3055\u308c\u305a\u30ea\u30bd\u30fc\u30b9\u3092\u903c\u8feb\u3057\u3066\u554f\u984c\u304c\u751f\u3058\u308b\u3053\u3068\u304c\u3042\u308b\u3002<\/p>\n<h2 class=\"title-border\"><strong>\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3057\u3066with\u3067\u7528\u3044\u308b<\/strong><\/h2>\n<p>with\u306f\u81ea\u8eab\u3067\u5b9a\u7fa9\u3057\u305f\u30af\u30e9\u30b9\u3067\u5229\u7528\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u3001<span class=\"pink_span\">with \u30af\u30e9\u30b9\u540d() as \u5909\u6570\u540d<\/span>\u3068\u8a18\u8ff0\u3059\u308b\u3002<br \/>\nwith\u3067\u5229\u7528\u3059\u308b\u30af\u30e9\u30b9\u306b\u306f\u3001<span class=\"pink_span\">__enter__\u3068__exit__<\/span>\u306e\uff12\u3064\u306e\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3059\u308b\u3002<\/p>\n<p>\u3053\u306e\u6642\u3001with\u306e\u5f8c\u306b<span class=\"pink_span\">\u30a4\u30f3\u30c7\u30f3\u30c8\u3057\u3066\u8a18\u8ff0\u3057\u305f\u51e6\u7406\u3092\u5b9f\u884c\u3059\u308b\u524d<\/spna>\u306b\u3001\u6307\u5b9a\u3057\u305f\u30af\u30e9\u30b9\u306e<span class=\"red_span\">__enter__\u3092\u547c\u3073\u51fa\u3057\u305d\u306e\u8fd4\u308a\u5024\u304cas\u3067\u6307\u5b9a\u3057\u305f\u5909\u6570\u306b\u683c\u7d0d\u3055\u308c\u308b<\/span>\u3001<span class=\"pink_span\">\u51e6\u7406\u7d42\u4e86\u5f8c\u306b<\/span>\u3001\u30af\u30e9\u30b9\u306e<span class=\"red_span\">__exit__<\/span>\u304c\u547c\u3070\u308c\u308b<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>with\u306e\u5b9f\u884c\u4f8b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nclass FileWriter(object):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self, file_name): <strong># \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.file_name = file_name<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __enter__(self): <strong># with\u958b\u59cb\u524d\u306b\u547c\u3073\u51fa\u3055\u308c\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.file = open(self.file_name, 'w')<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.file <strong># \u8fd4\u308a\u5024\u304cwith\u306e\u5909\u6570\u306b\u683c\u7d0d\u3055\u308c\u308b<\/strong><\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;def __exit__(self):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.file.close() <strong># with\u7d42\u4e86\u306b\u547c\u3073\u51fa\u3055\u308c\u308b<\/strong><br \/>\nwith FileWriter('test.txt') as file_writer:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;file_writer.write('hello world')<br \/>\n\u00a0 <strong># with\u3067\u30af\u30e9\u30b9\u3092\u4f5c\u6210\u3059\u308b\u3068\u3001\u307e\u305a__init__\u3092\u5b9f\u884c\u3001\u6b21\u306b__enter__\u3092\u5b9f\u884c\u3001\u51e6\u7406\u7d42\u4e86\u5f8c\u306b\u3001__exit__\u3092\u5b9f\u884c\u3059\u308b<br \/>\n\u00a0 # file_writer\u306b\u306f__enter__\u306e\u8fd4\u308a\u5024\u304c\u683c\u7d0d\u3055\u308c\u308b<\/strong>\n<\/div>\n<\/div>\n<h2 class=\"title-border\"><strong>with\u3092\u5229\u7528\u3059\u308b\u5834\u9762<\/strong><\/h2>\n<p>with\u3092\u3069\u306e\u3088\u3046\u306b\u5229\u7528\u3059\u308b\u304b\u3068\u3044\u3046\u3068\u3001<span class=\"pink_span\">\u9023\u7d9a\u3057\u305f\u4e00\u9023\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3057\u305f\u3044\u5834\u5408<\/span>\u3001<br \/>\n\u4f8b\u3048\u3070<\/p>\n<ul>\n<li>\u30d5\u30a1\u30a4\u30eb\u306e\u66f8\u304d\u8fbc\u307f\u51e6\u7406(<span style=\"text-decoration: underline;\">\u30d5\u30a1\u30a4\u30eb\u958b\u304f\u2192\u66f8\u304d\u8fbc\u3080\u2192\u30d5\u30a1\u30a4\u30eb\u9589\u3058\u308b<\/span>)<\/li>\n<li>DB\u3078\u306e\u30c7\u30fc\u30bf\u306e\u66f8\u304d\u8fbc\u307f\u51e6\u7406(<span style=\"text-decoration: underline;\">DB\u3078\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u5f35\u308b\u2192\u66f8\u304d\u8fbc\u3080\u2192\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u9589\u3058\u308b<\/span>)<\/li>\n<\/ul>\n<p>\u306a\u3069\u304c\u3042\u308b\u3002<\/p>\n<p>\u3053\u306e\u5834\u5408\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u5fd8\u308c\u3001\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u306e\u9589\u3058\u5fd8\u308c\u304c\u3042\u308b\u3068\u30e1\u30e2\u30ea\u304c\u89e3\u653e\u3055\u308c\u305a\u30ea\u30bd\u30fc\u30b9\u3092\u6d6a\u8cbb\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u30d5\u30a1\u30a4\u30eb\u5165\u51fa\u529b\u3001DB\u3078\u306e\u66f8\u304d\u8fbc\u307f\u5c02\u7528\u306e\u30af\u30e9\u30b9\u3092\u5225\u306b\u5b9a\u7fa9\u3057\u3066\u3001<\/p>\n<ul>\n<li><span style=\"text-decoration: underline;\">\u30d5\u30a1\u30a4\u30eb\u5165\u51fa\u529b\u3001DB\u3078\u306e\u66f8\u304d\u8fbc\u307f\u306fwith\u3067\u5229\u7528<\/span><\/li>\n<li><span style=\"text-decoration: underline;\">\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u3001DB\u3078\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u5f35\u308b\u51e6\u7406\u3092__enter__\u306b\u5b9a\u7fa9<\/span><\/li>\n<li><span style=\"text-decoration: underline;\">\u30d5\u30a1\u30a4\u30eb\u66f8\u304d\u8fbc\u307f\u3001DB\u3078\u306e\u66f8\u304d\u8fbc\u307f\u306f\u3001with\u306e\u4e2d\u306e\u51e6\u7406\u3067\u8a18\u8ff0<\/span><\/li>\n<li><span style=\"text-decoration: underline;\">\u30d5\u30a1\u30a4\u30eb\u3092\u9589\u3058\u308b\u3001\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u9589\u3058\u308b\u51e6\u7406\u3092__exit__\u306b\u5b9a\u7fa9<\/span><\/li>\n<\/ul>\n<p>\u306a\u3069\u306e\u4f7f\u3044\u65b9\u3092\u3059\u308b<\/p>\n<p>\u3053\u306e\u3088\u3046\u306b\u3059\u308b\u3068\u3001\u30d5\u30a1\u30a4\u30eb\u5165\u51fa\u529b\u3001DB\u3078\u306e\u66f8\u304d\u8fbc\u307f\u304c\u4fbf\u5229\u306b\u884c\u3048\u308b\u3088\u3046\u306b\u306a\u308b<\/p>\n<p>\u3061\u306a\u307f\u306b\u30d5\u30a1\u30a4\u30eb\u3078\u306e\u51fa\u529b\u306f\u4e00\u822c\u7684\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b<\/p>\n<div class=\"inside-advertise\"><strong>Python\u8b1b\u5ea7<\/strong>Python\u306b\u3064\u3044\u3066\u3082\u3063\u3068\u77e5\u308a\u305f\u3044\uff01\u3068\u3044\u3046\u304b\u305f\u306f\u3053\u3061\u3089\u3001Python\u306e\u57fa\u672c\u6587\u6cd5\u304b\u3089\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u3001\u5fdc\u7528\u30e9\u30a4\u30d6\u30e9\u30ea\u307e\u3067\u53ce\u9332\uff01\uff01<br \/><a class=\"udemy_python_lecture\" href=\"https:\/\/www.udemy.com\/course\/python-python\/?couponCode=EA7C756E9F9386712FC0\">\u30102020\u5e74\u7248\u3011: Python 3.8 \u30c7\u30fc\u30bf\u69cb\u9020\u3001\u6587\u6cd5\u3001\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u3001\u30e9\u30a4\u30d6\u30e9\u30ea\u3001\u5b8c\u5168\u30de\u30b9\u30bf\u30fc\u8b1b\u5ea7<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>with\u306e\u57fa\u672c \u3053\u3053\u3067\u306f\u3001python\u306ewith\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u8a18\u8f09\u3059\u308b\u3002 with\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3002 with \u30af\u30e9\u30b9 as a: \u00a0 \u00a0\u51e6\u7406 &nbsp; with\u306f\u30d5\u30a1\u30a4\u30eb\u5165\u51fa\u529b\u306a\u3069\u306e\u969b\u306b\u3088\u304f\u7528\u3044\u3089\u308c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[9,219],"tags":[14,87],"class_list":{"0":"post-414","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"category-it-programming","8":"tag-python","9":"tag-with","10":"entry"},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"home-middle":false,"home-top":false,"sidebar-thumbnail":false},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":0,"uagb_excerpt":"with\u306e\u57fa\u672c \u3053\u3053\u3067\u306f\u3001python\u306ewith\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u306b\u3064\u3044\u3066\u8a18\u8f09\u3059\u308b\u3002 with\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/comments?post=414"}],"version-history":[{"count":22,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":2781,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/414\/revisions\/2781"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}