{"id":17379,"date":"2024-01-21T05:06:58","date_gmt":"2024-01-20T20:06:58","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=17379"},"modified":"2024-01-21T05:07:00","modified_gmt":"2024-01-20T20:07:00","slug":"python-todo%e3%83%aa%e3%82%b9%e3%83%88-2","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=17379","title":{"rendered":"python todo\u30ea\u30b9\u30c8"},"content":{"rendered":"\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>import os\r\n\r\n# \u30d5\u30a1\u30a4\u30eb\u540d\r\ntodo_filename = &#39;todo.txt&#39;\r\n\r\n# TODO\u30ea\u30b9\u30c8\u3092\u8868\u793a\u3059\u308b\u95a2\u6570\r\ndef show_todo_list():\r\n    if os.path.exists(todo_filename):\r\n        with open(todo_filename, &#39;r&#39;) as file:\r\n            todo_list = file.readlines()\r\n            if todo_list:\r\n                print(&quot;TODO\u30ea\u30b9\u30c8:&quot;)\r\n                for i, item in enumerate(todo_list, start=1):\r\n                    print(f&quot;{i}. {item.strip()}&quot;)\r\n            else:\r\n                print(&quot;TODO\u30ea\u30b9\u30c8\u306f\u7a7a\u3067\u3059\u3002&quot;)\r\n    else:\r\n        print(&quot;TODO\u30ea\u30b9\u30c8\u306f\u307e\u3060\u4f5c\u6210\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002&quot;)\r\n\r\n# TODO\u30a2\u30a4\u30c6\u30e0\u3092\u8ffd\u52a0\u3059\u308b\u95a2\u6570\r\ndef add_todo_item(item):\r\n    with open(todo_filename, &#39;a&#39;) as file:\r\n        file.write(item + &#39;\\n&#39;)\r\n    print(f&quot;&#39;{item}&#39; \u3092TODO\u30ea\u30b9\u30c8\u306b\u8ffd\u52a0\u3057\u307e\u3057\u305f\u3002&quot;)\r\n\r\n# TODO\u30a2\u30a4\u30c6\u30e0\u3092\u524a\u9664\u3059\u308b\u95a2\u6570\r\ndef remove_todo_item(item_number):\r\n    if os.path.exists(todo_filename):\r\n        with open(todo_filename, &#39;r&#39;) as file:\r\n            todo_list = file.readlines()\r\n        if 1 &lt;= item_number &lt;= len(todo_list):\r\n            removed_item = todo_list.pop(item_number - 1).strip()\r\n            with open(todo_filename, &#39;w&#39;) as file:\r\n                file.writelines(todo_list)\r\n            print(f&quot;&#39;{removed_item}&#39; \u3092TODO\u30ea\u30b9\u30c8\u304b\u3089\u524a\u9664\u3057\u307e\u3057\u305f\u3002&quot;)\r\n        else:\r\n            print(&quot;\u6307\u5b9a\u3055\u308c\u305f\u756a\u53f7\u306eTODO\u30a2\u30a4\u30c6\u30e0\u306f\u5b58\u5728\u3057\u307e\u305b\u3093\u3002&quot;)\r\n    else:\r\n        print(&quot;TODO\u30ea\u30b9\u30c8\u306f\u307e\u3060\u4f5c\u6210\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002&quot;)\r\n\r\n# \u30e1\u30a4\u30f3\u30e1\u30cb\u30e5\u30fc\u3092\u8868\u793a\u3059\u308b\u95a2\u6570\r\ndef main_menu():\r\n    while True:\r\n        print(&quot;\\n\u30e1\u30cb\u30e5\u30fc:&quot;)\r\n        print(&quot;1. TODO\u30ea\u30b9\u30c8\u3092\u8868\u793a&quot;)\r\n        print(&quot;2. TODO\u30a2\u30a4\u30c6\u30e0\u3092\u8ffd\u52a0&quot;)\r\n        print(&quot;3. TODO\u30a2\u30a4\u30c6\u30e0\u3092\u524a\u9664&quot;)\r\n        print(&quot;4. \u7d42\u4e86&quot;)\r\n        \r\n        choice = input(&quot;\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044: &quot;)\r\n\r\n        if choice == &#39;1&#39;:\r\n            show_todo_list()\r\n        elif choice == &#39;2&#39;:\r\n            item = input(&quot;\u8ffd\u52a0\u3059\u308bTODO\u30a2\u30a4\u30c6\u30e0\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044: &quot;)\r\n            add_todo_item(item)\r\n        elif choice == &#39;3&#39;:\r\n            show_todo_list()\r\n            item_number = int(input(&quot;\u524a\u9664\u3059\u308bTODO\u30a2\u30a4\u30c6\u30e0\u306e\u756a\u53f7\u3092\u5165\u529b\u3057\u3066\u304f\u3060\u3055\u3044: &quot;))\r\n            remove_todo_item(item_number)\r\n        elif choice == &#39;4&#39;:\r\n            print(&quot;\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u7d42\u4e86\u3057\u307e\u3059\u3002&quot;)\r\n            break\r\n        else:\r\n            print(&quot;\u7121\u52b9\u306a\u9078\u629e\u3067\u3059\u3002\u518d\u5ea6\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002&quot;)\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n    main_menu()\r\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[4,65],"tags":[3,39],"class_list":["post-17379","post","type-post","status-publish","format-standard","hentry","category-programming","category-python","tag-programming","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17379"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17379\/revisions"}],"predecessor-version":[{"id":17380,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/17379\/revisions\/17380"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17379"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}