1
Folder         2018
Sub-folder1         01
Sub-folder2            01
Sub-folder3               1234
                          4567
file                          abd.jason
file                          fghg.jason

I need to use regex in these files How can I loop the entire folder Sub-folder1 is the month Sub-folder2 is the date of each month Sub-folder3 is the Submission number(And I need to grasp this number as the name into the data frame

Then I need to use Regex to find something and attache the valued follow SubmissionNumber in DataFrame

How can I loop 3 level of subfolder

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Yong Li
  • 11
  • 1

2 Answers2

0

Have a look at os.walk, see if that answers your question. If you are having trouble with a more specific part of it after that, ask a new question =)

Stephen C
  • 1,966
  • 1
  • 16
  • 30
  • import os rootdir = " " for dirName, subdirList, fileList in os.walk(rootdir): if len(dirName.split("/"[-1])) > 5: print(dirName.split("/")[-1]) for fname in fileList: if fname != '.DS_Store': print('\t%s' % fname) This is my code, but I print out some subfolder, but I noly need the subfolder 3 – Yong Li Nov 15 '18 at 23:13
0

I try another time

import os

rootdir = "/Users/yongli/Downloads/Data/JSON Output/Submission/2018"

for dirName, subdirList, fileList in os.walk(rootdir):
    # screen the path splited using '/' and count the length
    if len(dirName.split("/")) > 10: 
        print(dirName.split("/")[-1])
        for fname in fileList:
            if fname != '.DS_Store':
            print('\t%s' % fname)
Yong Li
  • 11
  • 1