withæã試ãã¾ããã Pythonãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ããå¼ç¨ with æã¯ããããã¯ã®å®è¡ããã³ã³ããã¹ãããã¼ã¸ã£ã«ãã£ã¦å®ç¾©ãããã¡ã½ããã§ã©ããããããã«ä½¿ããã¾ãï¼ 3.4.9 ã»ã¯ã·ã§ã³ãåç §ãã¦ãã ããï¼ãããã«ããããããã try...except...finally å©ç¨ãã¿ã¼ã³ãã«ãã»ã«åãã¦ä¾¿å©ã«åå©ç¨ãããã¨ãã§ãã¾ãã ä½ãæ¸ãã¦ãããã¤ãã¤ãå¤ãã¾ãããã å ·ä½çãªä½¿ãéã¨ãã¦ãopen() 㨠close()ã対ã«ãªã£ã¦ããã©ã¤ãã©ãªãã使ããã¾ãã ãã¡ã¤ã«ã®ãªã¼ãã³ã¨ã¯ãã¼ãºã§è©¦ãã¦ã¿ã¾ãããã def main(): print('[1]') #ãã¡ã¤ã«ãªã¼ãã³ with open('test.txt', 'w') as fp: #fpã«ãã¡ã¤ã«ãªãã¸ã§ã¯ããå ¥ã fp.write('hello world\n') #ãã¡ã¤ã«ã«æ¸ã込㿠fp.
{{#tags}}- {{label}}
{{/tags}}