如何清理这个目录遍历?

huangapple go评论109阅读模式
英文:

How to clean up this directory traversal?

问题

  1. for maps_dir in self.directories:
  2. for map_dir in os.listdir(maps_dir):
  3. if fails_check1(map_dir):
  4. continue
  5. for filename in os.listdir(os.path.join(maps_dir, map_dir)):
  6. if not filename.endswith(".json"):
  7. continue
  8. file_path = os.path.join(map_dir, filename)
  9. if os.path.isfile(file_path):
  10. with open(file_path, encoding="utf-8") as demo_json:
  11. demo_data: Game = json.load(demo_json)
  12. match_id = os.path.splitext(filename)[0]
  13. if fails_check2(match_id):
  14. continue
  15. self.do_stuff(demo_data, match_id)
英文:

I have the following directory structure that i want to traverse and call a function on each (loaded) file if the directories and file pass certain criteria

  1. self.directories[0]
  2. └───maps_dir_1
  3. file1.json
  4. file2.txt
  5. file3.json
  6. └───subfolder1
  7. file111.txt
  8. file112.txt
  9. ..
  10. └───maps_dir_1
  11. file4.json
  12. file5.txt
  13. file6.json

I have a working way but it is super nested and ugly and i am sure there has to be a cleaner way, but i am unsure what it is.

  1. for maps_dir in self.directories:
  2. for map_dir in os.listdir(maps_dir):
  3. if fails_check1(map_dir):
  4. continue
  5. for filename in os.listdir(os.path.join(maps_dir, map_dir)):
  6. if not filename.endswith(".json"):
  7. continue
  8. file_path = os.path.join(map_dir, filename)
  9. if os.path.isfile(file_path):
  10. with open(file_path, encoding="utf-8") as demo_json:
  11. demo_data: Game = json.load(demo_json)
  12. match_id = os.path.splitext(filename)[0]
  13. if fails_check2(match_id):
  14. continue
  15. self.do_stuff(demo_data, match_id)

答案1

得分: 1

这是一个示例。我使用:

  • pathlib.Path - 以获得更好的抽象
  • 生成器 - 以减少嵌套,并将迭代分解为概念上的部分
  1. import json
  2. from pathlib import Path
  3. directories = ['some/list', 'of/directories']
  4. # 生成器函数
  5. def map_dirs():
  6. for path in directories:
  7. for map_dir in Path(path).iterdir():
  8. if check1(map_dir):
  9. yield map_dir
  10. # 生成器表达式的替代方案,如果您不介意较长的行
  11. json_files = (file for map_dir in map_dirs() for file in map_dir.glob('*.json') if check2(file.stem))
  12. # 概念上合并为单个循环
  13. for json_file in json_files:
  14. with open(json_file) as demo_json:
  15. demo_data = json.load(demo_json)
  16. do_stuff(demo_data, json_file.stem)

我使用了布尔取反的 check1()check2(),因为在肯定表达上读起来更自然,依我看来(而且更短)。

英文:

Here's an example. I use:

  • pathlib.Path - for its better abstractions
  • generators - to reduce nesting and break up the iteration into conceptual parts
  1. import json
  2. from pathlib import Path
  3. directories = ['some/list', 'of/directories']
  4. # generator function
  5. def map_dirs():
  6. for path in directories:
  7. for map_dir in Path(path).iterdir():
  8. if check1(map_dir):
  9. yield map_dir
  10. # generator expression alternative, if you don't mind the long line
  11. json_files = (file for map_dir in map_dirs() for file in map_dir.glob('*.json') if check2(file.stem))
  12. # conceptually flattented into a single loop
  13. for json_file in json_files:
  14. with open(json_file) as demo_json:
  15. demo_data = json.load(demo_json)
  16. do_stuff(demo_data, json_file.stem)

I use the boolean-flipped check1() and check2() b/c expressing in the positive reads more naturally IMO (and it's shorter).

huangapple
  • 本文由 发表于 2023年5月18日 04:10:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定