有个文件夹 '/tmp',如何用 Python 统计其下文件数量?

1 revotu Jun 27, 2017 Python 文件操作相关问题: http://www.revotu.com/python-file-operation-related-issues.html >>> import os >>> DIR = '/tmp' >>> print len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))]) 如统计文件夹数量,用 os.path.isdir(path)做判断语句。 |
2 hemoely Jun 27, 2017 用 python 套一个 shell |
3 yucongo Jun 28, 2017 from pathlib import Path # Py3 自带 dir_path = '.' print(len(list(Path(dir_path).iterdir())) |