python:pathlib

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
次のリビジョン両方とも次のリビジョン
python:pathlib [2020/02/01 20:14] – [参考文献] ともやんpython:pathlib [2020/03/01 14:06] ともやん
行 1: 行 1:
-====== pathlib, glob ======+====== pathlib, glob, os ====== 
 +<code python> 
 +#!/usr/bin/env python3 
 +# -*- coding: utf-8 -*- 
 +import os 
 +import glob 
 +import timeit 
 + 
 +def walk(): 
 +    #result = [[file for file in files if file.endswith('.py')] for path, dirs, files in os.walk('.')] 
 +    result = [] 
 +    for path, dirs, files in os.walk('.'): 
 +        for file in files: 
 +            if file.endswith('.py'): 
 +                result.append(file) 
 +    return result 
 + 
 +def iglob(): 
 +    result = [] 
 +    for file in glob.iglob('**/*', recursive=True): 
 +        if file.endswith('.py'): 
 +            result.append(file) 
 +    return result 
 + 
 +def iglob2(): 
 +    result = [] 
 +    for file in glob.iglob('**/*.py', recursive=True): 
 +        result.append(file) 
 +    return result 
 + 
 +def main(): 
 +    # os.chdir('./'
 +    num_of_exec = 10 
 +    print(timeit.timeit(walk, number=num_of_exec) / num_of_exec) 
 +    print(timeit.timeit(iglob, number=num_of_exec) / num_of_exec) 
 +    print(timeit.timeit(iglob2, number=num_of_exec) / num_of_exec) 
 + 
 +if __name__ == '__main__': 
 +    main() 
 +</code> 
 +<code> 
 +$ ./dir_search.py 
 +1.3243966199999704 
 +1.4991581099999167 
 +1.61260242999997 
 +$ time find . -name '*.py' > /dev/null 
 + 
 +real    0m0.523s 
 +user    0m0.020s 
 +sys     0m0.077s 
 +</code>
  
 ===== 参考文献 ===== ===== 参考文献 =====
 [[https://docs.python.org/ja/3/library/glob.html|glob --- Unix 形式のパス名のパターン展開 — Python 3.8.1 ドキュメント]]\\ [[https://docs.python.org/ja/3/library/glob.html|glob --- Unix 形式のパス名のパターン展開 — Python 3.8.1 ドキュメント]]\\
 [[https://stackoverflow.com/questions/20638040/glob-exclude-pattern|python - glob exclude pattern - Stack Overflow]]\\ [[https://stackoverflow.com/questions/20638040/glob-exclude-pattern|python - glob exclude pattern - Stack Overflow]]\\
-[[https://stackoverflow.com/questions/20638040/glob-exclude-pattern/36295481|python - glob exclude pattern - Stack Overflow]]\\ 
 [[https://github.com/python/cpython/blob/master/Lib/pathlib.py|cpython/pathlib.py at master · python/cpython]]\\ [[https://github.com/python/cpython/blob/master/Lib/pathlib.py|cpython/pathlib.py at master · python/cpython]]\\
 +[[https://stackoverflow.com/questions/50948391/whats-the-fastest-way-to-recursively-search-for-files-in-python/50950952|What's the fastest way to recursively search for files in python? - Stack Overflow]]\\
 +[[https://qiita.com/amowwee/items/e63b3610ea750f7dba1b|Pythonでフォルダ内のファイルリストを取得する - Qiita]]\\
  
  • python/pathlib.txt
  • 最終更新: 2023/05/27 09:00
  • by ともやん