[{"data":1,"prerenderedAt":563},["ShallowReactive",2],{"content-\u002Fcontents\u002Fpython-util":3,"surroundPost-\u002Fcontents\u002Fpython-util":554},{"id":4,"title":5,"body":6,"createdAt":542,"description":543,"draft":544,"extension":545,"meta":546,"navigation":200,"path":547,"seo":548,"stem":549,"tags":550,"thumbnail":552,"updatedAt":542,"__hash__":553},"contents\u002Fcontents\u002Fpython-util.md","Pythonでよく使うものをコピペ用にまとめてみた",{"type":7,"value":8,"toc":535},"minimark",[9,13,16,19,22,26,29,45,48,174,184,222,238,276,283,329,335,338,341,360,370,373,376,383,386,429,432,451,455,458,461,490,496,519,522,525,528,531],[10,11,12],"p",{},"Python で何度も同じことを調べてしまうので、この記事にまとめます！",[10,14,15],{},"使っている Python バージョンは 3.6~3.7 です。",[10,17,18],{},"汎用的なものをまとめるので基本的にどのバージョンでも動くと思います。",[10,20,21],{},"紹介するものは標準で使えるものなので特に新しくインストールするものはないです。",[23,24,25],"h2",{"id":25},"ファイルパスリスト抽出",[10,27,28],{},"ある拡張子やあるフォルダ配下のファイルのパス一覧を取得するコードです。",[10,30,31,32,36,37,44],{},"使うのは",[33,34,35],"code",{},"glob","というモジュールです。\n（参考：",[38,39,43],"a",{"href":40,"rel":41},"https:\u002F\u002Fdocs.python.org\u002Fja\u002F3.7\u002Flibrary\u002Fglob.html",[42],"nofollow","glob --- Unix 形式のパス名のパターン展開","）",[10,46,47],{},"今、以下のようなディレクトリ構造があるとします。",[49,50,55],"pre",{"className":51,"code":52,"language":53,"meta":54,"style":54},"language-bash shiki shiki-themes github-dark",".\n├── files\n│   ├── file1.txt\n│   ├── file2.txt\n│   ├── files2\n│   │   ├── file1-Copy1.txt\n│   │   └── file2-Copy1.txt\n│   └── files3\n│       ├── file1-Copy1.txt\n│       └── file2-Copy1.txt\n└── py_util.ipynb\n","bash","",[33,56,57,66,77,89,99,109,122,135,145,155,165],{"__ignoreMap":54},[58,59,62],"span",{"class":60,"line":61},"line",1,[58,63,65],{"class":64},"sDLfK",".\n",[58,67,69,73],{"class":60,"line":68},2,[58,70,72],{"class":71},"svObZ","├──",[58,74,76],{"class":75},"sU2Wk"," files\n",[58,78,80,83,86],{"class":60,"line":79},3,[58,81,82],{"class":71},"│",[58,84,85],{"class":75},"   ├──",[58,87,88],{"class":75}," file1.txt\n",[58,90,92,94,96],{"class":60,"line":91},4,[58,93,82],{"class":71},[58,95,85],{"class":75},[58,97,98],{"class":75}," file2.txt\n",[58,100,102,104,106],{"class":60,"line":101},5,[58,103,82],{"class":71},[58,105,85],{"class":75},[58,107,108],{"class":75}," files2\n",[58,110,112,114,117,119],{"class":60,"line":111},6,[58,113,82],{"class":71},[58,115,116],{"class":75},"   │",[58,118,85],{"class":75},[58,120,121],{"class":75}," file1-Copy1.txt\n",[58,123,125,127,129,132],{"class":60,"line":124},7,[58,126,82],{"class":71},[58,128,116],{"class":75},[58,130,131],{"class":75},"   └──",[58,133,134],{"class":75}," file2-Copy1.txt\n",[58,136,138,140,142],{"class":60,"line":137},8,[58,139,82],{"class":71},[58,141,131],{"class":75},[58,143,144],{"class":75}," files3\n",[58,146,148,150,153],{"class":60,"line":147},9,[58,149,82],{"class":71},[58,151,152],{"class":75},"       ├──",[58,154,121],{"class":75},[58,156,158,160,163],{"class":60,"line":157},10,[58,159,82],{"class":71},[58,161,162],{"class":75},"       └──",[58,164,134],{"class":75},[58,166,168,171],{"class":60,"line":167},11,[58,169,170],{"class":71},"└──",[58,172,173],{"class":75}," py_util.ipynb\n",[10,175,176,179,180,183],{},[33,177,178],{},"files","配下の",[33,181,182],{},".txt","のファイルパスを知りたい場合は以下のコードでできます。",[49,185,189],{"className":186,"code":187,"language":188,"meta":54,"style":54},"language-py shiki shiki-themes github-dark","from glob import glob\n\npath = \".\u002Ffiles\u002F*.txt\"\npath_list = glob(path)\nprint(path_list)\n# ['.\u002Ffiles\u002Ffile2.txt', '.\u002Ffiles\u002Ffile1.txt']\n","py",[33,190,191,196,202,207,212,217],{"__ignoreMap":54},[58,192,193],{"class":60,"line":61},[58,194,195],{},"from glob import glob\n",[58,197,198],{"class":60,"line":68},[58,199,201],{"emptyLinePlaceholder":200},true,"\n",[58,203,204],{"class":60,"line":79},[58,205,206],{},"path = \".\u002Ffiles\u002F*.txt\"\n",[58,208,209],{"class":60,"line":91},[58,210,211],{},"path_list = glob(path)\n",[58,213,214],{"class":60,"line":101},[58,215,216],{},"print(path_list)\n",[58,218,219],{"class":60,"line":111},[58,220,221],{},"# ['.\u002Ffiles\u002Ffile2.txt', '.\u002Ffiles\u002Ffile1.txt']\n",[10,223,224,226,227,230,231,234,235,237],{},[33,225,178],{},"の配下の",[33,228,229],{},"files2","と",[33,232,233],{},"files3","の",[33,236,182],{},"のパスも取得したい場合は以下のようにします。",[49,239,241],{"className":186,"code":240,"language":188,"meta":54,"style":54},"path = \".\u002Ffiles\u002F*\u002F*.txt\"\npath_list = glob(path)\nprint(path_list)\n#['.\u002Ffiles\u002Ffiles2\u002Ffile1-Copy1.txt',\n# '.\u002Ffiles\u002Ffiles2\u002Ffile2-Copy1.txt',\n# '.\u002Ffiles\u002Ffiles3\u002Ffile1-Copy1.txt',\n# '.\u002Ffiles\u002Ffiles3\u002Ffile2-Copy1.txt']\n",[33,242,243,248,252,256,261,266,271],{"__ignoreMap":54},[58,244,245],{"class":60,"line":61},[58,246,247],{},"path = \".\u002Ffiles\u002F*\u002F*.txt\"\n",[58,249,250],{"class":60,"line":68},[58,251,211],{},[58,253,254],{"class":60,"line":79},[58,255,216],{},[58,257,258],{"class":60,"line":91},[58,259,260],{},"#['.\u002Ffiles\u002Ffiles2\u002Ffile1-Copy1.txt',\n",[58,262,263],{"class":60,"line":101},[58,264,265],{},"# '.\u002Ffiles\u002Ffiles2\u002Ffile2-Copy1.txt',\n",[58,267,268],{"class":60,"line":111},[58,269,270],{},"# '.\u002Ffiles\u002Ffiles3\u002Ffile1-Copy1.txt',\n",[58,272,273],{"class":60,"line":124},[58,274,275],{},"# '.\u002Ffiles\u002Ffiles3\u002Ffile2-Copy1.txt']\n",[10,277,278,279,282],{},"また、",[33,280,281],{},"recursive=True","にすると再起的にたどってファイルパスを取得することも可能です。",[49,284,286],{"className":186,"code":285,"language":188,"meta":54,"style":54},"path = \".\u002Ffiles\u002F**\u002F*.txt\"\npath_list = glob(path, recursive=True)\nprint(path_list)\n#['.\u002Ffiles\u002Ffile2.txt',\n# '.\u002Ffiles\u002Ffile1.txt',\n# '.\u002Ffiles\u002Ffiles2\u002Ffile1-Copy1.txt',\n# '.\u002Ffiles\u002Ffiles2\u002Ffile2-Copy1.txt',\n# '.\u002Ffiles\u002Ffiles3\u002Ffile1-Copy1.txt',\n# '.\u002Ffiles\u002Ffiles3\u002Ffile2-Copy1.txt']\n",[33,287,288,293,298,302,307,312,317,321,325],{"__ignoreMap":54},[58,289,290],{"class":60,"line":61},[58,291,292],{},"path = \".\u002Ffiles\u002F**\u002F*.txt\"\n",[58,294,295],{"class":60,"line":68},[58,296,297],{},"path_list = glob(path, recursive=True)\n",[58,299,300],{"class":60,"line":79},[58,301,216],{},[58,303,304],{"class":60,"line":91},[58,305,306],{},"#['.\u002Ffiles\u002Ffile2.txt',\n",[58,308,309],{"class":60,"line":101},[58,310,311],{},"# '.\u002Ffiles\u002Ffile1.txt',\n",[58,313,314],{"class":60,"line":111},[58,315,316],{},"# '.\u002Ffiles\u002Ffiles2\u002Ffile1-Copy1.txt',\n",[58,318,319],{"class":60,"line":124},[58,320,265],{},[58,322,323],{"class":60,"line":137},[58,324,270],{},[58,326,327],{"class":60,"line":147},[58,328,275],{},[10,330,331,334],{},[33,332,333],{},"**","を使うと間の階層にあるディ レクトリ全てを指定できます。",[23,336,337],{"id":337},"フォルダを新しく作る",[10,339,340],{},"単純ですがけっこう使います。",[49,342,344],{"className":186,"code":343,"language":188,"meta":54,"style":54},"import os\n\nos.makedirs(path, exist_ok=True)\n",[33,345,346,351,355],{"__ignoreMap":54},[58,347,348],{"class":60,"line":61},[58,349,350],{},"import os\n",[58,352,353],{"class":60,"line":68},[58,354,201],{"emptyLinePlaceholder":200},[58,356,357],{"class":60,"line":79},[58,358,359],{},"os.makedirs(path, exist_ok=True)\n",[10,361,362,365,366,369],{},[33,363,364],{},"os.makedirs(path)","だけだと、すでにフォルダが存在する場合はエラーになるので、",[33,367,368],{},"exist_ok=True","が必要になります。",[23,371,372],{"id":372},"ファイルコピーと移動",[10,374,375],{},"ファイルのパス抽出とフォルダ作成と合わせて使うことが多いです。",[10,377,378,379,382],{},"使うモジュールは",[33,380,381],{},"shutil","です。",[10,384,385],{},"コピーしたい場合は以下のように書きます。",[49,387,389],{"className":186,"code":388,"language":188,"meta":54,"style":54},"import shutil\n\ndef copy_files(path_list, copy_path):\n    for p in path_list:\n        shutil.copy(p, copy_path)\n\n# 1行でも書ける\n# copied_list = [shutil.copy(p, copy_path) for p in path_list]\n",[33,390,391,396,400,405,410,415,419,424],{"__ignoreMap":54},[58,392,393],{"class":60,"line":61},[58,394,395],{},"import shutil\n",[58,397,398],{"class":60,"line":68},[58,399,201],{"emptyLinePlaceholder":200},[58,401,402],{"class":60,"line":79},[58,403,404],{},"def copy_files(path_list, copy_path):\n",[58,406,407],{"class":60,"line":91},[58,408,409],{},"    for p in path_list:\n",[58,411,412],{"class":60,"line":101},[58,413,414],{},"        shutil.copy(p, copy_path)\n",[58,416,417],{"class":60,"line":111},[58,418,201],{"emptyLinePlaceholder":200},[58,420,421],{"class":60,"line":124},[58,422,423],{},"# 1行でも書ける\n",[58,425,426],{"class":60,"line":137},[58,427,428],{},"# copied_list = [shutil.copy(p, copy_path) for p in path_list]\n",[10,430,431],{},"移動の場合もコピーと同様にかけます。",[49,433,435],{"className":186,"code":434,"language":188,"meta":54,"style":54},"def move_files(path_list, move_path):\n    for p in path_list:\n        shutil.move(p, move_path)\n",[33,436,437,442,446],{"__ignoreMap":54},[58,438,439],{"class":60,"line":61},[58,440,441],{},"def move_files(path_list, move_path):\n",[58,443,444],{"class":60,"line":68},[58,445,409],{},[58,447,448],{"class":60,"line":79},[58,449,450],{},"        shutil.move(p, move_path)\n",[23,452,454],{"id":453},"フォルダファイル削除","フォルダ、ファイル削除",[10,456,457],{},"簡単です。",[10,459,460],{},"ファイルの場合はパスを指定して以下で実行できます。",[49,462,466],{"className":463,"code":464,"language":465,"meta":54,"style":54},"language-python shiki shiki-themes github-dark","import os\n\npath = \".\u002Ffiles\u002Ffile1.txt\"\n\nos.remove(path)\n","python",[33,467,468,472,476,481,485],{"__ignoreMap":54},[58,469,470],{"class":60,"line":61},[58,471,350],{},[58,473,474],{"class":60,"line":68},[58,475,201],{"emptyLinePlaceholder":200},[58,477,478],{"class":60,"line":79},[58,479,480],{},"path = \".\u002Ffiles\u002Ffile1.txt\"\n",[58,482,483],{"class":60,"line":91},[58,484,201],{"emptyLinePlaceholder":200},[58,486,487],{"class":60,"line":101},[58,488,489],{},"os.remove(path)\n",[10,491,492,493,495],{},"空じゃないフォルダを削除する場合は先ほど登場した",[33,494,381],{},"を使います。",[49,497,499],{"className":186,"code":498,"language":188,"meta":54,"style":54},"import shutil\n\npath = \".\u002Ffiles\u002Ffiles2\"\nshutil.rmtree(path)\n",[33,500,501,505,509,514],{"__ignoreMap":54},[58,502,503],{"class":60,"line":61},[58,504,395],{},[58,506,507],{"class":60,"line":68},[58,508,201],{"emptyLinePlaceholder":200},[58,510,511],{"class":60,"line":79},[58,512,513],{},"path = \".\u002Ffiles\u002Ffiles2\"\n",[58,515,516],{"class":60,"line":91},[58,517,518],{},"shutil.rmtree(path)\n",[23,520,521],{"id":521},"まとめ",[10,523,524],{},"自分がよく使うものをまとめてみました。",[10,526,527],{},"それぞれ調べればすぐに出てきますが、一箇所にまとめてあると便利です。",[10,529,530],{},"今後も思いついたらこのページに追加します。",[532,533,534],"style",{},"html pre.shiki code .sDLfK, html code.shiki .sDLfK{--shiki-default:#79B8FF}html pre.shiki code .svObZ, html code.shiki .svObZ{--shiki-default:#B392F0}html pre.shiki code .sU2Wk, html code.shiki .sU2Wk{--shiki-default:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":54,"searchDepth":68,"depth":68,"links":536},[537,538,539,540,541],{"id":25,"depth":68,"text":25},{"id":337,"depth":68,"text":337},{"id":372,"depth":68,"text":372},{"id":453,"depth":68,"text":454},{"id":521,"depth":68,"text":521},"2020-08-14","Pythonで同じことをよくググってしまうので、よく使うものをこのページにすぐにコピペできるようにまとめました。汎用的なもの重視でなるべく短くかけるように心がけてます。",false,"md",{},"\u002Fcontents\u002Fpython-util",{"title":5,"description":543},"contents\u002Fpython-util",[551],"Python","\u002Fimg\u002Ftwitter-card.png","dvCZ1JmjuqJf16VeUr7uCR8uJ2KXGlxTJYzECeKTEfc",[555,559],{"title":556,"path":557,"stem":558,"children":-1},"意外と知らなかったPythonの便利な書き方集","\u002Fcontents\u002Fpython","contents\u002Fpython",{"title":560,"path":561,"stem":562,"children":-1},"Pythonでログ出力する方法について","\u002Fcontents\u002Fpythonlogger","contents\u002Fpythonlogger",1784936718503]