# Python iterate over path tree.
## 安装
你可以通过 [pypi](https://pypi.org/project/iterdir/) 安装
```console
pip install -U iterdir
```
## 使用
### 用作模块
提供了一个函数 `iterdir()` 可用于遍历目录树
```python
>>> from iterdir import iterdir
>>> help(iterdir)
Help on function iterdir in module iterdir:
iterdir(top=None, /, topdown: Optional[bool] = True, min_depth: int = 1, max_depth: int = 1, predicate: Optional[collections.abc.Callable[..., Optional[bool]]] = None, onerror: bool | collections.abc.Callable[[OSError], bool] = False, follow_symlinks: bool = False) -> collections.abc.Iterator
遍历目录树
:param top: 根路径,默认为当前目录。
:param topdown: 如果是 True,自顶向下深度优先搜索;如果是 False,自底向上深度优先搜索;如果是 None,广度优先搜索。
:param min_depth: 最小深度,小于 0 时不限。参数 `top` 本身的深度为 0,它的直接跟随路径的深度是 1,以此类推。
:param max_depth: 最大深度,小于 0 时不限。
:param predicate: 调用以筛选遍历得到的路径。可接受的参数与参数 `top` 的类型一致,参见 `:return:` 部分。
:param onerror: 处理 OSError 异常。如果是 True,抛出异常;如果是 False,忽略异常;如果是调用,以异常为参数调用之。
:param follow_symlinks: 是否跟进符号连接(如果为否,则会把符号链接视为文件,即使它指向目录)。
:return: 遍历得到的路径的迭代器。参数 `top` 的类型:
- 如果是 iterdir.DirEntry,则是 iterdir.DirEntry 实例的迭代器
- 如果是 pathlib.Path,则是 pathlib.Path 实例的迭代器
- 否则,得到 `os.fspath(top)` 相同类型的实例的迭代器
```
### 用作命令
提供一个命令行工具,用于导出目录树
```console
$ python -m iterdir -h
usage: __main__ [-h] [-m MIN_DEPTH] [-M MAX_DEPTH]
[-k [{inode,name,path,relpath,isdir,islink,stat} ...]]
[-t {log,json,csv}] [-d DUMP] [-de] [-s SELECT] [-se]
[-o OUTPUT_FILE]
[-hs [{sha256,sha3_512,sha1,sha512_256,md5,ripemd160,sha512,md5-sha1,sha3_256,sha384,sha3_384,sha512_224,sha224,sm3,shake_128,blake2s,sha3_224,blake2b,shake_256,crc32} ...]]
[-dfs] [-fl] [-v]
[path]
目录树信息遍历导出
positional arguments:
path 文件夹路径,默认为当前工作目录
options:
-h, --help show this help message and exit
-m MIN_DEPTH, --min-depth MIN_DEPTH
最小深度,默认值 0,小于 0 时不限
-M MAX_DEPTH, --max-depth MAX_DEPTH
最大深度,默认值 -1,小于 0 时不限
-k [{inode,name,path,relpath,isdir,islink,stat} ...], --keys [{inode,name,path,relpath,isdir,islink,stat} ...]
选择输出的 key,默认输出所有可选值
-t {log,json,csv}, --output-type {log,json,csv}
输出类型,默认为 log
- log 每行输出一条数据,每条数据输出为一个 json 的 object
- json 输出一个 json 的 list,每条数据输出为一个 json 的 object
- csv 输出一个 csv,第 1 行为表头,以后每行输出一条数据
-d DUMP, --dump DUMP (优先级高于 -k/--keys、-hs/--hashes、-t/--output-type) 调用以导出数据,如果有返回值则再行输出,尾部会添加一个 b'
'。
如果结果 result 是
- None,跳过
- bytes,输出
- 其它,先调用 `bytes(str(result), 'utf-8')`,再输出
提供一个表达式(会注入一个变量 path,类型是 pathlib.Path)或函数(会传入一个参数,类型是 pathlib.Path)
-de, --dump-exec 对 -d/--dump 传入的代码用 exec 运行,其中必须存在名为 dump 的函数。否则,视为表达式或 lambda 函数
-s SELECT, --select SELECT
对路径进行筛选,提供一个表达式(会注入一个变量 path,类型是 pathlib.Path)或函数(会传入一个参数,类型是 pathlib.Path)
-se, --select-exec 对 -s/--select 传入的代码用 exec 运行,其中必须存在名为 select 的函数。否则,视为表达式或 lambda 函数
-o OUTPUT_FILE, --output-file OUTPUT_FILE
保存到文件,此时命令行会输出进度条
-hs [{sha256,sha3_512,sha1,sha512_256,md5,ripemd160,sha512,md5-sha1,sha3_256,sha384,sha3_384,sha512_224,sha224,sm3,shake_128,blake2s,sha3_224,blake2b,shake_256,crc32} ...], --hashes [{sha256,sha3_512,sha1,sha512_256,md5,ripemd160,sha512,md5-sha1,sha3_256,sha384,sha3_384,sha512_224,sha224,sm3,shake_128,blake2s,sha3_224,blake2b,shake_256,crc32} ...]
计算文件的哈希值,可以选择多个算法
-dfs, --depth-first 使用深度优先搜索,否则使用广度优先
-fl, --follow-symlinks
跟进符号连接,否则会把符号链接视为文件,即使它指向目录
-v, --version 输出版本
```
Raw data
{
"_id": null,
"home_page": "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/iterdir",
"name": "iterdir",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "iterdir, traverse",
"author": "ChenyangGao",
"author_email": "wosiwujm@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/24/2f/52ce96865f7316861fc0a33ec2eb23a6e0b012b2d8206cb61e74d616116c/iterdir-0.0.5.3.tar.gz",
"platform": null,
"description": "# Python iterate over path tree.\n\n## \u5b89\u88c5\n\n\u4f60\u53ef\u4ee5\u901a\u8fc7 [pypi](https://pypi.org/project/iterdir/) \u5b89\u88c5\n\n```console\npip install -U iterdir\n```\n\n## \u4f7f\u7528\n\n### \u7528\u4f5c\u6a21\u5757\n\n\u63d0\u4f9b\u4e86\u4e00\u4e2a\u51fd\u6570 `iterdir()` \u53ef\u7528\u4e8e\u904d\u5386\u76ee\u5f55\u6811\n\n```python\n>>> from iterdir import iterdir\n>>> help(iterdir)\n\nHelp on function iterdir in module iterdir:\n\niterdir(top=None, /, topdown: Optional[bool] = True, min_depth: int = 1, max_depth: int = 1, predicate: Optional[collections.abc.Callable[..., Optional[bool]]] = None, onerror: bool | collections.abc.Callable[[OSError], bool] = False, follow_symlinks: bool = False) -> collections.abc.Iterator\n \u904d\u5386\u76ee\u5f55\u6811\n \n :param top: \u6839\u8def\u5f84\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d\u76ee\u5f55\u3002\n :param topdown: \u5982\u679c\u662f True\uff0c\u81ea\u9876\u5411\u4e0b\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff1b\u5982\u679c\u662f False\uff0c\u81ea\u5e95\u5411\u4e0a\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff1b\u5982\u679c\u662f None\uff0c\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\u3002\n :param min_depth: \u6700\u5c0f\u6df1\u5ea6\uff0c\u5c0f\u4e8e 0 \u65f6\u4e0d\u9650\u3002\u53c2\u6570 `top` \u672c\u8eab\u7684\u6df1\u5ea6\u4e3a 0\uff0c\u5b83\u7684\u76f4\u63a5\u8ddf\u968f\u8def\u5f84\u7684\u6df1\u5ea6\u662f 1\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\n :param max_depth: \u6700\u5927\u6df1\u5ea6\uff0c\u5c0f\u4e8e 0 \u65f6\u4e0d\u9650\u3002\n :param predicate: \u8c03\u7528\u4ee5\u7b5b\u9009\u904d\u5386\u5f97\u5230\u7684\u8def\u5f84\u3002\u53ef\u63a5\u53d7\u7684\u53c2\u6570\u4e0e\u53c2\u6570 `top` \u7684\u7c7b\u578b\u4e00\u81f4\uff0c\u53c2\u89c1 `:return:` \u90e8\u5206\u3002\n :param onerror: \u5904\u7406 OSError \u5f02\u5e38\u3002\u5982\u679c\u662f True\uff0c\u629b\u51fa\u5f02\u5e38\uff1b\u5982\u679c\u662f False\uff0c\u5ffd\u7565\u5f02\u5e38\uff1b\u5982\u679c\u662f\u8c03\u7528\uff0c\u4ee5\u5f02\u5e38\u4e3a\u53c2\u6570\u8c03\u7528\u4e4b\u3002\n :param follow_symlinks: \u662f\u5426\u8ddf\u8fdb\u7b26\u53f7\u8fde\u63a5\uff08\u5982\u679c\u4e3a\u5426\uff0c\u5219\u4f1a\u628a\u7b26\u53f7\u94fe\u63a5\u89c6\u4e3a\u6587\u4ef6\uff0c\u5373\u4f7f\u5b83\u6307\u5411\u76ee\u5f55\uff09\u3002\n \n :return: \u904d\u5386\u5f97\u5230\u7684\u8def\u5f84\u7684\u8fed\u4ee3\u5668\u3002\u53c2\u6570 `top` \u7684\u7c7b\u578b\uff1a\n - \u5982\u679c\u662f iterdir.DirEntry\uff0c\u5219\u662f iterdir.DirEntry \u5b9e\u4f8b\u7684\u8fed\u4ee3\u5668\n - \u5982\u679c\u662f pathlib.Path\uff0c\u5219\u662f pathlib.Path \u5b9e\u4f8b\u7684\u8fed\u4ee3\u5668\n - \u5426\u5219\uff0c\u5f97\u5230 `os.fspath(top)` \u76f8\u540c\u7c7b\u578b\u7684\u5b9e\u4f8b\u7684\u8fed\u4ee3\u5668\n```\n\n### \u7528\u4f5c\u547d\u4ee4\n\n\u63d0\u4f9b\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u7528\u4e8e\u5bfc\u51fa\u76ee\u5f55\u6811\n\n```console\n$ python -m iterdir -h\nusage: __main__ [-h] [-m MIN_DEPTH] [-M MAX_DEPTH]\n [-k [{inode,name,path,relpath,isdir,islink,stat} ...]]\n [-t {log,json,csv}] [-d DUMP] [-de] [-s SELECT] [-se]\n [-o OUTPUT_FILE]\n [-hs [{sha256,sha3_512,sha1,sha512_256,md5,ripemd160,sha512,md5-sha1,sha3_256,sha384,sha3_384,sha512_224,sha224,sm3,shake_128,blake2s,sha3_224,blake2b,shake_256,crc32} ...]]\n [-dfs] [-fl] [-v]\n [path]\n\n\u76ee\u5f55\u6811\u4fe1\u606f\u904d\u5386\u5bfc\u51fa\n\npositional arguments:\n path \u6587\u4ef6\u5939\u8def\u5f84\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\n\noptions:\n -h, --help show this help message and exit\n -m MIN_DEPTH, --min-depth MIN_DEPTH\n \u6700\u5c0f\u6df1\u5ea6\uff0c\u9ed8\u8ba4\u503c 0\uff0c\u5c0f\u4e8e 0 \u65f6\u4e0d\u9650\n -M MAX_DEPTH, --max-depth MAX_DEPTH\n \u6700\u5927\u6df1\u5ea6\uff0c\u9ed8\u8ba4\u503c -1\uff0c\u5c0f\u4e8e 0 \u65f6\u4e0d\u9650\n -k [{inode,name,path,relpath,isdir,islink,stat} ...], --keys [{inode,name,path,relpath,isdir,islink,stat} ...]\n \u9009\u62e9\u8f93\u51fa\u7684 key\uff0c\u9ed8\u8ba4\u8f93\u51fa\u6240\u6709\u53ef\u9009\u503c\n -t {log,json,csv}, --output-type {log,json,csv}\n \u8f93\u51fa\u7c7b\u578b\uff0c\u9ed8\u8ba4\u4e3a log\n - log \u6bcf\u884c\u8f93\u51fa\u4e00\u6761\u6570\u636e\uff0c\u6bcf\u6761\u6570\u636e\u8f93\u51fa\u4e3a\u4e00\u4e2a json \u7684 object\n - json \u8f93\u51fa\u4e00\u4e2a json \u7684 list\uff0c\u6bcf\u6761\u6570\u636e\u8f93\u51fa\u4e3a\u4e00\u4e2a json \u7684 object\n - csv \u8f93\u51fa\u4e00\u4e2a csv\uff0c\u7b2c 1 \u884c\u4e3a\u8868\u5934\uff0c\u4ee5\u540e\u6bcf\u884c\u8f93\u51fa\u4e00\u6761\u6570\u636e\n -d DUMP, --dump DUMP (\u4f18\u5148\u7ea7\u9ad8\u4e8e -k/--keys\u3001-hs/--hashes\u3001-t/--output-type) \u8c03\u7528\u4ee5\u5bfc\u51fa\u6570\u636e\uff0c\u5982\u679c\u6709\u8fd4\u56de\u503c\u5219\u518d\u884c\u8f93\u51fa\uff0c\u5c3e\u90e8\u4f1a\u6dfb\u52a0\u4e00\u4e2a b'\n '\u3002\n \u5982\u679c\u7ed3\u679c result \u662f\n - None\uff0c\u8df3\u8fc7\n - bytes\uff0c\u8f93\u51fa\n - \u5176\u5b83\uff0c\u5148\u8c03\u7528 `bytes(str(result), 'utf-8')`\uff0c\u518d\u8f93\u51fa\n \u63d0\u4f9b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff08\u4f1a\u6ce8\u5165\u4e00\u4e2a\u53d8\u91cf path\uff0c\u7c7b\u578b\u662f pathlib.Path\uff09\u6216\u51fd\u6570\uff08\u4f1a\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u7c7b\u578b\u662f pathlib.Path\uff09 \n -de, --dump-exec \u5bf9 -d/--dump \u4f20\u5165\u7684\u4ee3\u7801\u7528 exec \u8fd0\u884c\uff0c\u5176\u4e2d\u5fc5\u987b\u5b58\u5728\u540d\u4e3a dump \u7684\u51fd\u6570\u3002\u5426\u5219\uff0c\u89c6\u4e3a\u8868\u8fbe\u5f0f\u6216 lambda \u51fd\u6570\n -s SELECT, --select SELECT\n \u5bf9\u8def\u5f84\u8fdb\u884c\u7b5b\u9009\uff0c\u63d0\u4f9b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff08\u4f1a\u6ce8\u5165\u4e00\u4e2a\u53d8\u91cf path\uff0c\u7c7b\u578b\u662f pathlib.Path\uff09\u6216\u51fd\u6570\uff08\u4f1a\u4f20\u5165\u4e00\u4e2a\u53c2\u6570\uff0c\u7c7b\u578b\u662f pathlib.Path\uff09\n -se, --select-exec \u5bf9 -s/--select \u4f20\u5165\u7684\u4ee3\u7801\u7528 exec \u8fd0\u884c\uff0c\u5176\u4e2d\u5fc5\u987b\u5b58\u5728\u540d\u4e3a select \u7684\u51fd\u6570\u3002\u5426\u5219\uff0c\u89c6\u4e3a\u8868\u8fbe\u5f0f\u6216 lambda \u51fd\u6570\n -o OUTPUT_FILE, --output-file OUTPUT_FILE\n \u4fdd\u5b58\u5230\u6587\u4ef6\uff0c\u6b64\u65f6\u547d\u4ee4\u884c\u4f1a\u8f93\u51fa\u8fdb\u5ea6\u6761\n -hs [{sha256,sha3_512,sha1,sha512_256,md5,ripemd160,sha512,md5-sha1,sha3_256,sha384,sha3_384,sha512_224,sha224,sm3,shake_128,blake2s,sha3_224,blake2b,shake_256,crc32} ...], --hashes [{sha256,sha3_512,sha1,sha512_256,md5,ripemd160,sha512,md5-sha1,sha3_256,sha384,sha3_384,sha512_224,sha224,sm3,shake_128,blake2s,sha3_224,blake2b,shake_256,crc32} ...]\n \u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u53ef\u4ee5\u9009\u62e9\u591a\u4e2a\u7b97\u6cd5\n -dfs, --depth-first \u4f7f\u7528\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u5426\u5219\u4f7f\u7528\u5e7f\u5ea6\u4f18\u5148\n -fl, --follow-symlinks\n \u8ddf\u8fdb\u7b26\u53f7\u8fde\u63a5\uff0c\u5426\u5219\u4f1a\u628a\u7b26\u53f7\u94fe\u63a5\u89c6\u4e3a\u6587\u4ef6\uff0c\u5373\u4f7f\u5b83\u6307\u5411\u76ee\u5f55\n -v, --version \u8f93\u51fa\u7248\u672c\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "python iterate over path tree.",
"version": "0.0.5.3",
"project_urls": {
"Homepage": "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/iterdir",
"Repository": "https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/iterdir"
},
"split_keywords": [
"iterdir",
" traverse"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8ee10e50f2bc998fd33a2564392397351af7f6d8b7c9d92bf6e0033337de3ef3",
"md5": "12de8afe1c95715141c1370e6abe8ad4",
"sha256": "cd630f1a075fbdb1589f9700624e3545e80cb5a9d56a56613a95fe32460a03a7"
},
"downloads": -1,
"filename": "iterdir-0.0.5.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "12de8afe1c95715141c1370e6abe8ad4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 11281,
"upload_time": "2024-06-26T05:12:50",
"upload_time_iso_8601": "2024-06-26T05:12:50.424093Z",
"url": "https://files.pythonhosted.org/packages/8e/e1/0e50f2bc998fd33a2564392397351af7f6d8b7c9d92bf6e0033337de3ef3/iterdir-0.0.5.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "242f52ce96865f7316861fc0a33ec2eb23a6e0b012b2d8206cb61e74d616116c",
"md5": "0203e66be58b2e9b80ef273f23ad1bb6",
"sha256": "12090dea1d330f05e6b221f0713e6fc8d07093b5468a6b09517fa69f4767aa41"
},
"downloads": -1,
"filename": "iterdir-0.0.5.3.tar.gz",
"has_sig": false,
"md5_digest": "0203e66be58b2e9b80ef273f23ad1bb6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 8356,
"upload_time": "2024-06-26T05:12:52",
"upload_time_iso_8601": "2024-06-26T05:12:52.473109Z",
"url": "https://files.pythonhosted.org/packages/24/2f/52ce96865f7316861fc0a33ec2eb23a6e0b012b2d8206cb61e74d616116c/iterdir-0.0.5.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-26 05:12:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ChenyangGao",
"github_project": "web-mount-packs",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "iterdir"
}