cwl6
cwl6

Reputation: 27

Getting data from different folders

I have two sets of json files store in two folders(named firstdata and seconddata) separately. I am trying to read all files in that two folders and put it into two arrays separately. Here is the code I did:

directory = os.path.normpath("D:\Python\project")
for subdir, dir, file in os.walk(directory):
    if subdir == 'D:\Python\project\firstdata':
        for f in file:
            if f.endswith(".json"):
                fread=open(os.path.join(subdir, f),'r')
                a = fread.next().replace('\n','').split(',')

                for line in a:
                    b = line.replace('.','').replace('\n','').replace('"','').split(': ')

            print "___________________________________________________________________"
            fread.close()   

However it ignores (if subdir == 'D:\Python\project\firstdata': ) and get nothing at the end, can anyone helps?

Upvotes: 0

Views: 36

Answers (1)

Remolten
Remolten

Reputation: 2682

You are interpreting things wrong. See the docs for **os.walk**.

The 3 variables for your for loop should be root, dirs, and files, in that order.

dirs and files are lists, of the directories and files in the current directory respectively. root is the current directory you are in.

subdir is being ignored because you are using os.walk incorrectly.

Upvotes: 2

Related Questions