差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
python:pathlib [2020/03/28 08:35] – ともやん | python:pathlib [2023/05/27 09:00] (現在) – [glob.iglob() の実装] ともやん | ||
---|---|---|---|
行 1: | 行 1: | ||
- | < | + | ====== pathlib, glob.iglob(), os.walk() |
- | < | + | |
- | #mincode pre { | + | ===== os.walk() の実装 ===== |
- | /*height: 300px;*/ | + | < |
- | overflow: scroll; | + | |
- | overflow-x: hidden; | + | |
- | font-size: 10px; | + | |
- | } | + | |
- | # | + | |
- | 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; | + | |
- | } | + | |
- | </ | + | |
- | </ | + | |
- | ====== pathlib, glob, os ====== | + | |
===== glob.iglob() の実装 ===== | ===== glob.iglob() の実装 ===== | ||
- | **glob.iglob(pathname, | + | < |
- | **os.scandir(path=' | + | < |
- | < | + | ここで注意が必要なのは、 **pattern** は **__Unix Shell Style__** のパターンであって正規表現は利用できない。\\ |
+ | < | ||
+ | \\ | ||
+ | **OK パターン**\\ | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | ' | ||
+ | </ | ||
+ | **NG パターン**\\ | ||
+ | <code python> | ||
+ | >>> | ||
+ | >>> | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | <WRAP mincode_long> | ||
<code python python38/ | <code python python38/ | ||
""" | """ | ||
行 209: | 行 200: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | ===== os.scandir() の実装 ===== | ||
+ | [[https:// | ||
+ | < | ||
+ | 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 関数を使用します。 | ||
+ | </ | ||
+ | <WRAP mincode_long> | ||
+ | <code c cpython/ | ||
+ | /*[clinic input] | ||
+ | os.scandir | ||
+ | |||
+ | path : path_t(nullable=True, | ||
+ | |||
+ | Return an iterator of DirEntry objects for given path. | ||
+ | |||
+ | path can be specified as either str, bytes, or a path-like object. | ||
+ | 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(" | ||
+ | path-> | ||
+ | return NULL; | ||
+ | } | ||
+ | |||
+ | PyObject *ScandirIteratorType = get_posix_state(module)-> | ||
+ | iterator = PyObject_New(ScandirIterator, | ||
+ | if (!iterator) | ||
+ | return NULL; | ||
+ | |||
+ | #ifdef MS_WINDOWS | ||
+ | iterator-> | ||
+ | #else | ||
+ | iterator-> | ||
+ | #endif | ||
+ | |||
+ | memcpy(& | ||
+ | /* Move the ownership to iterator-> | ||
+ | path-> | ||
+ | path-> | ||
+ | |||
+ | #ifdef MS_WINDOWS | ||
+ | iterator-> | ||
+ | |||
+ | path_strW = join_path_filenameW(iterator-> | ||
+ | if (!path_strW) | ||
+ | goto error; | ||
+ | |||
+ | Py_BEGIN_ALLOW_THREADS | ||
+ | iterator-> | ||
+ | Py_END_ALLOW_THREADS | ||
+ | |||
+ | PyMem_Free(path_strW); | ||
+ | |||
+ | if (iterator-> | ||
+ | path_error(& | ||
+ | goto error; | ||
+ | } | ||
+ | #else /* POSIX */ | ||
+ | errno = 0; | ||
+ | #ifdef HAVE_FDOPENDIR | ||
+ | if (path-> | ||
+ | /* closedir() closes the FD, so we duplicate it */ | ||
+ | fd = _Py_dup(path-> | ||
+ | if (fd == -1) | ||
+ | goto error; | ||
+ | |||
+ | Py_BEGIN_ALLOW_THREADS | ||
+ | iterator-> | ||
+ | Py_END_ALLOW_THREADS | ||
+ | } | ||
+ | else | ||
+ | #endif | ||
+ | { | ||
+ | if (iterator-> | ||
+ | path_str = iterator-> | ||
+ | else | ||
+ | path_str = " | ||
+ | |||
+ | Py_BEGIN_ALLOW_THREADS | ||
+ | iterator-> | ||
+ | Py_END_ALLOW_THREADS | ||
+ | } | ||
+ | |||
+ | if (!iterator-> | ||
+ | path_error(& | ||
+ | #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; | ||
+ | } | ||
+ | </ | ||
+ | </ | ||
+ | [[https:// | ||
===== パフォーマンス比較 ===== | ===== パフォーマンス比較 ===== | ||
行 267: | 行 382: | ||
===== 参考文献 ===== | ===== 参考文献 ===== | ||
- | [[https:// | + | [[https:// |
+ | [[https:// | ||
[[https:// | [[https:// | ||
[[https:// | [[https:// | ||
行 273: | 行 389: | ||
[[https:// | [[https:// | ||
[[https:// | [[https:// | ||
+ | [[https:// | ||
+ | [[https:// | ||