-6

Trying to get folders name (without file names) from a directory and covert them into JSON format. So after 4 iteration of the folders, i will try to get userinput from the user. My current working code is:

import glob
import json
import io

in0  = input("Enter Userinput0:\n")
in1  = input("Enter Userinput1:\n") 
in2  = input("Enter Userinput2:\n")
in3  = input("Enter Userinput3:\n")
name = "somename"

def path_to_dict(path):

data = {os.path.basename(path): {}}
if os.path.isdir(path):
count = 0
    for x in os.listdir(path):
        data[os.path.basename(path)] =[path_to_dict(os.path.join(path,x))]
    count += 1
    if count == 4:
        break
    return data
path_to_dict('some path')
d = {"Userinput0": in0}
d[name] = {data: {"Userinput1": in1,"Userinput2": in2,"Userinput3": in3, }}
j = json.dumps(d, indent=5)
print (j)

Output for the above code:

TypeError: unhashable type: 'dict'

My tree structure:

|-Folder1
|--Subfolder       
|---Subfolder
|----Subfolder 
|-----Subfolder

Output JSON format i am trying to get:

{
  "Userinput0": "x.x", 
  "Somename": {
    "Folder1": {
      "subfolder": {
         "subfolder": {
            "subfolder": {
                "subfolder": {
                    "Userinput1": "x",
                    "Userinput2": "Y",
                    "Userinput3": Z
                }
            }
        }
     }
   }
}

I have edited my question and current code. Still i am not able to attain desired output So could anyone please help me to achieve the output.

Anonymous
  • 113
  • 1
  • 1
  • 10
  • It is unclear how you get to the result that those `Userinput` values should be set at that exact nested path. It's somewhat trivial to build a dict structure for folder paths, see https://stackoverflow.com/q/14692690/476. But you could easily have the folders `['/foo/bar/', '/foo/baz/']`. Now what? Where do the `Userinput`s go? What's the expected result? – deceze Apr 05 '22 at 06:41
  • @deceze I have updated my question. As per my output structure, i should not get ['folder name'], and **userinput** values should be nested at the exact path as per the output format – Anonymous Apr 05 '22 at 13:42
  • And the question is: how do you programatically determine "the exact path as per the output format"?! If you get an arbitrarily nested set of arbitrary folders from `glob`, *where* do you put the user inputs?! – deceze Apr 05 '22 at 13:45
  • As per my code, i have set the userinputs in a path, i have to traverse through the folders name in specific path `d[name] = {data: {"Userinput1": in1,"Userinput2": in2,"Userinput3": in3, }}` here userinputs are already in path, in place of **data** i am trying to pull names of the folders – Anonymous Apr 05 '22 at 13:59
  • You have a bunch of folders on disk. You want to create a JSON structure that represents those folders. E.g. if you have the folders `foo/bar/baz/`, you want `{'foo': {'bar': {'baz': {}}}}`, correct? Now, how do you determine that the user inputs should be at `{'foo': {'bar': {'baz': {'Userinput1': ...}}}}`? Because the folders on your disk that you get through `glob` might also lead to `{'foo': {}, 'bar': {'baz': {}}}`! Now what? – deceze Apr 05 '22 at 14:02
  • Whatever you said as Eg is correct. According to my folder structure, e.g. `foo/bar/baz/quz` so exactly after 4th subfolder, i am trying to put the user inputs values. So if we use `glob` it will lead to `{'foo': {}, 'bar': {'baz': {}}}`. so do we have any alternate option then – Anonymous Apr 05 '22 at 14:35
  • How do you determine "4th subfolder"? Is "4" a hardcoded number? Can this rule be expressed any other way? And yes, what alternatives *do* you have?! It's *your* rules that you're making up here, you need to be able to express them clearly. – deceze Apr 05 '22 at 14:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243617/discussion-between-anonymous-and-deceze). – Anonymous Apr 05 '22 at 14:39
  • 1
    @Anonymous since the question is still lacking clarity, add the clarified details in the question rather than continuing them on chat imo – Abhinav Mathur Apr 08 '22 at 13:49
  • @AbhinavMathur i have edited my question. Can you please have a look at it – Anonymous Apr 10 '22 at 19:22
  • Just copy and paste your example code, could you make it run? – Ynjxsjmh Apr 11 '22 at 12:36

1 Answers1

0

def path_to_dict(path):

data = {os.path.basename(path): {
                "Userinput1": in1,
                "Userinput2": in2,
                "Userinput3": in3,
            }}
if os.path.isdir(path):
    count = 0
    for x in os.listdir(path):
        data[os.path.basename(path)] =path_to_dict(os.path.join(path,x))
        count += 1
        if count == 4:
            break
    
    return data

print(path_to_dict)

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 12 '22 at 05:12