差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン両方とも次のリビジョン | ||
python:pathlib [2020/02/01 20:14] – [参考文献] ともやん | python:pathlib [2020/03/01 14:06] – ともやん | ||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== pathlib, glob ====== | + | ====== pathlib, glob, os ====== |
+ | <code python> | ||
+ | # | ||
+ | # -*- coding: utf-8 -*- | ||
+ | import os | ||
+ | import glob | ||
+ | import timeit | ||
+ | |||
+ | def walk(): | ||
+ | #result = [[file for file in files if file.endswith(' | ||
+ | result = [] | ||
+ | for path, dirs, files in os.walk(' | ||
+ | for file in files: | ||
+ | if file.endswith(' | ||
+ | result.append(file) | ||
+ | return result | ||
+ | |||
+ | def iglob(): | ||
+ | result = [] | ||
+ | for file in glob.iglob(' | ||
+ | if file.endswith(' | ||
+ | result.append(file) | ||
+ | return result | ||
+ | |||
+ | def iglob2(): | ||
+ | result = [] | ||
+ | for file in glob.iglob(' | ||
+ | result.append(file) | ||
+ | return result | ||
+ | |||
+ | def main(): | ||
+ | # os.chdir(' | ||
+ | num_of_exec = 10 | ||
+ | print(timeit.timeit(walk, | ||
+ | print(timeit.timeit(iglob, | ||
+ | print(timeit.timeit(iglob2, | ||
+ | |||
+ | if __name__ == ' | ||
+ | main() | ||
+ | </ | ||
+ | < | ||
+ | $ ./ | ||
+ | 1.3243966199999704 | ||
+ | 1.4991581099999167 | ||
+ | 1.61260242999997 | ||
+ | $ time find . -name ' | ||
+ | |||
+ | real 0m0.523s | ||
+ | user 0m0.020s | ||
+ | sys | ||
+ | </ | ||
===== 参考文献 ===== | ===== 参考文献 ===== | ||
[[https:// | [[https:// | ||
[[https:// | [[https:// | ||
- | [[https:// | ||
[[https:// | [[https:// | ||
+ | [[https:// | ||
+ | [[https:// | ||