python:pathlib

差分

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

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
python:pathlib [2020/03/30 12:04] – [参考文献] ともやんpython:pathlib [2023/05/27 09:00] (現在) – [glob.iglob() の実装] ともやん
行 1: 行 1:
-<html> +====== pathlib, glob.iglob(), os.walk() ======
-  <style> +
-    #mincode pre { +
-      /*height: 300px;*/ +
-      overflow: scroll; +
-      overflow-x: hidden; +
-      font-size: 10px; +
-    } +
-    #mincode_long pre { +
-      height: 400px; +
-      overflow: scroll; +
-      overflow-x: hidden; +
-      font-size: 10px; +
-    } +
-    #mintbl table { +
-      font-size: 12px; +
-    } +
-    .dokuwiki .plugin_wrap table { +
-      width: auto; +
-    } +
-    #result pre { +
-      /*height: 300px;*/ +
-      overflow: scroll; +
-      overflow-x: hidden; +
-      font-size: 10px; +
-    } +
-  </style> +
-</html> +
-====== pathlib, glob, os ======+
  
 ===== os.walk() の実装 ===== ===== os.walk() の実装 =====
-**glob.iglob(pathname*recursive=False)** は、内部的には **os.scandir(path='.')** によって処理される。\\+<html><code>os.walk(toptopdown=True, onerror=Nonefollowlinks=False)</code></html> は、内部的には <html><code>os.scandir(path='.')</code></html> によって処理される。\\
  
 ===== glob.iglob() の実装 ===== ===== glob.iglob() の実装 =====
-**glob.iglob(pathname, *, recursive=False)** は、内部的には **os.scandir(path='.')** によって処理される。\\ +<html><code>glob.iglob(pathname, *, recursive=False)</code></html> は、内部的には <html><code>os.scandir(path='.')</code></html> によって処理される。\\ 
-**os.scandir(path='.')** によって取得された内容は **list()** 化されて **fnmatch.filter(names, pattern)** によってフィルター処理される。\\ +<html><code>os.scandir(path='.')</code></html> によって取得された内容は **list()** 化されて <html><code>fnmatch.filter(names, pattern)</code></html> によってフィルター処理される。\\ 
-**fnmatch.filter(names, pattern)** の **pattern** は **fnmatch.translate(pattern)** で正規表現に変換してから **re.compile()** される。ここで注意が必要なのは、 **pattern** は **__Unix Shell Style__** のパターンであって正規表現は利用できない。\\+ここで注意が必要なのは、 **pattern** は **__Unix Shell Style__** のパターンであって正規表現は利用できない。\\ 
 +<html><code>fnmatch.filter(names, pattern)</code></html> の **pattern** は <html><code>fnmatch.translate(pattern)</code></html> で正規表現に変換してから <html><code>re.compile()</code></html> される。\\
 \\ \\
 **OK パターン**\\ **OK パターン**\\
行 50: 行 23:
 </code> </code>
  
-<WRAP prewrap 100% #mincode_long>+<WRAP mincode_long>
 <code python python38/Lib/glob.py> <code python python38/Lib/glob.py>
 """Filename globbing utility.""" """Filename globbing utility."""
行 227: 行 200:
 </code> </code>
 </WRAP> </WRAP>
 +
 +===== os.scandir() の実装 =====
 +[[https://docs.python.org/3/library/os.html#os.scandir|os — Miscellaneous operating system interfaces — Python 3.8.2 documentation]]\\
 +<note>
 +Note On Unix-based systems, scandir() uses the system’s opendir() and readdir() functions. On Windows, it uses the Win32 FindFirstFileW and FindNextFileW functions.\\
 +\\
 +ノート(翻訳) Unix ベースのシステムでは、scandir() はシステムの opendir() 関数と readdir() 関数を使用します。 Windows では、Win32 FindFirstFileW 関数と FindNextFileW 関数を使用します。
 +</note>
 +<WRAP mincode_long>
 +<code c cpython/Modules/posixmodule.c>
 +/*[clinic input]
 +os.scandir
 +
 +    path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
 +
 +Return an iterator of DirEntry objects for given path.
 +
 +path can be specified as either str, bytes, or a path-like object.  If path
 +is bytes, the names of yielded DirEntry objects will also be bytes; in
 +all other circumstances they will be str.
 +
 +If path is None, uses the path='.'.
 +[clinic start generated code]*/
 +
 +static PyObject *
 +os_scandir_impl(PyObject *module, path_t *path)
 +/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
 +{
 +    ScandirIterator *iterator;
 +#ifdef MS_WINDOWS
 +    wchar_t *path_strW;
 +#else
 +    const char *path_str;
 +#ifdef HAVE_FDOPENDIR
 +    int fd = -1;
 +#endif
 +#endif
 +
 +    if (PySys_Audit("os.scandir", "O",
 +                    path->object ? path->object : Py_None) < 0) {
 +        return NULL;
 +    }
 +
 +    PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
 +    iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
 +    if (!iterator)
 +        return NULL;
 +
 +#ifdef MS_WINDOWS
 +    iterator->handle = INVALID_HANDLE_VALUE;
 +#else
 +    iterator->dirp = NULL;
 +#endif
 +
 +    memcpy(&iterator->path, path, sizeof(path_t));
 +    /* Move the ownership to iterator->path */
 +    path->object = NULL;
 +    path->cleanup = NULL;
 +
 +#ifdef MS_WINDOWS
 +    iterator->first_time = 1;
 +
 +    path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
 +    if (!path_strW)
 +        goto error;
 +
 +    Py_BEGIN_ALLOW_THREADS
 +    iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
 +    Py_END_ALLOW_THREADS
 +
 +    PyMem_Free(path_strW);
 +
 +    if (iterator->handle == INVALID_HANDLE_VALUE) {
 +        path_error(&iterator->path);
 +        goto error;
 +    }
 +#else /* POSIX */
 +    errno = 0;
 +#ifdef HAVE_FDOPENDIR
 +    if (path->fd != -1) {
 +        /* closedir() closes the FD, so we duplicate it */
 +        fd = _Py_dup(path->fd);
 +        if (fd == -1)
 +            goto error;
 +
 +        Py_BEGIN_ALLOW_THREADS
 +        iterator->dirp = fdopendir(fd);
 +        Py_END_ALLOW_THREADS
 +    }
 +    else
 +#endif
 +    {
 +        if (iterator->path.narrow)
 +            path_str = iterator->path.narrow;
 +        else
 +            path_str = ".";
 +
 +        Py_BEGIN_ALLOW_THREADS
 +        iterator->dirp = opendir(path_str);
 +        Py_END_ALLOW_THREADS
 +    }
 +
 +    if (!iterator->dirp) {
 +        path_error(&iterator->path);
 +#ifdef HAVE_FDOPENDIR
 +        if (fd != -1) {
 +            Py_BEGIN_ALLOW_THREADS
 +            close(fd);
 +            Py_END_ALLOW_THREADS
 +        }
 +#endif
 +        goto error;
 +    }
 +#endif
 +
 +    return (PyObject *)iterator;
 +
 +error:
 +    Py_DECREF(iterator);
 +    return NULL;
 +}
 +</code>
 +</WRAP>
 +[[https://github.com/python/cpython/blob/master/Modules/posixmodule.c|cpython/Modules/posixmodule.c - github.com]] より\\
  
 ===== パフォーマンス比較 ===== ===== パフォーマンス比較 =====
行 293: 行 390:
 [[https://stackoverflow.com/questions/24812253/how-can-i-capture-return-value-with-python-timeit-module|How can I capture return value with Python timeit module? - Stack Overflow]]\\ [[https://stackoverflow.com/questions/24812253/how-can-i-capture-return-value-with-python-timeit-module|How can I capture return value with Python timeit module? - Stack Overflow]]\\
 [[https://living-sun.com/ja/python/705207-quicker-to-oswalk-or-glob-python-traversal-glob-oswalk-directory-walk.html|os.walkやglobにすばやく - Python、トラバーサル、グロブ、os.walk、ディレクトリウォーク]]\\ [[https://living-sun.com/ja/python/705207-quicker-to-oswalk-or-glob-python-traversal-glob-oswalk-directory-walk.html|os.walkやglobにすばやく - Python、トラバーサル、グロブ、os.walk、ディレクトリウォーク]]\\
 +[[https://note.dokeep.jp/post/csharp-fast-enumerate-file/|[C#] 高速でファイルとフォルダを列挙する - ざこノート]]\\
  
  • python/pathlib.1585537464.txt.gz
  • 最終更新: 2020/03/30 12:04
  • by ともやん