0

I'm iterating through a directory containing a number of JSON files and sub-directories and .txt files. I need to do this until there are no more JSON files remaining in this root directory.

How do I express this condition?

quamrana
  • 37,849
  • 12
  • 53
  • 71
  • 1
    What have you done so far? – Chen A. Sep 19 '17 at 10:39
  • So, I've got a for loop which iterates through a list of all files in the root directory. Within this for loop, there is an if statement which needs to check if there are more JSON files in this directory. If there are, then the iteration can stop. – entity without identity Sep 19 '17 at 10:40
  • You need to update the question with this code. – quamrana Sep 19 '17 at 10:41
  • How do you tell a JSON file from another type of file? By filename extension? If so you could try `glob.glob('*json')` to get a list of files in the current directory with extension ".json". Or `os.walk()` if you need to descend into the subdirectories (not clear in your question whether you actually want that or not). – mhawke Sep 19 '17 at 10:41
  • Please edit the question to include the script you have written so far. For sub folders, you should take a look at `os.walk()`. – Martin Evans Sep 19 '17 at 10:41
  • First, convert the JSON to python-dict (import json; json.loads()), and due to the keys in dict are iterable, you can use for key in dict: to tranverse all the keys(including your so-called misisng ones). – Menglong Li Sep 19 '17 at 10:41
  • 1
    Can you share this code in your question? It would help us help you – Chen A. Sep 19 '17 at 10:42

0 Answers0